Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun sending attachment with RestSharp

I'm using RestSharp to try and send an attachment with the Mailgun API. I have tried attaching from both a file in the system using a hardcoded path and also from a binary file stored in the database using ToArray() method on the varbinary(MAX) (SQL Server) property both with no success.

The attachment technically sends, but when the email arrives in my inbox the file size is always roughly 302bytes big and is always corrupt. I have tried 3 different files and get the same problem each time.

The rest of the email sends, delivers and displays fine. It's just the attachments that are broken.

Breakdown of code:

// Doesnt work(Data property is varbinary(MAX)
request.AddFileBytes("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");

// Also doesnt work(Data property is varbinary(MAX)
request.AddFile("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");

// Also doesnt work        
var path = @"D:\Template.pdf";
request.AddFile("attachment",path,"multipart/form-data");
like image 266
Sinmok Avatar asked Jun 03 '16 18:06

Sinmok


People also ask

How to send an email using Mailgun API?

There are two ways to send emails using Mailgun API: You can pass the components of the messages such as To , From, Subject, HTML and text parts, attachments, etc. Mailgun will build a MIME representation of the message and send it. This is the preferred method.

What is the maximum message size Mailgun can send?

Sending options (those prefixed by o:, h:, or v:) are limited to 16 kB in total. Mailgun supports maximum messages size of 25MB. You can also use good old SMTP to send messages. But you will have to specify all advanced sending options via MIME headers

How does Mailgun work with multiple domains?

You can set up multiple domains and choose if you want to share IPs or build your own reputation. To protect your email headers, Mailgun requires DKIM and SPF be added to the account, and that they pass their checks at the authentication and alignment level. All of these things work together to help you with email delivery.

How do I set up a PHP wrapper script for Mailgun?

Now to the good stuff: setting up a PHP wrapper script for Mailgun. First, we’ll use the package manager and then see examples of how to apply it. PEAR is a community driven initiative that lets us reuse PHP code components, while the dependent code is also added and maintained. First, you will need PHP, and the package manager to continue.


2 Answers

This code works:

public static void Main(string[] args)
{
    Console.WriteLine(SendSimpleMessage().Content.ToString());
    Console.ReadLine();
}

public static IRestResponse SendSimpleMessage()
{
    var path1 = @"C:\Users\User\Pictures\website preview";
    var fileName = "Learn.png";
    RestClient client = new RestClient();
    client.BaseUrl = new Uri("https://api.mailgun.net/v3");
    client.Authenticator =
        new HttpBasicAuthenticator("api",
                                    "key-934345306fead7de0296ec2fb96a143");
    RestRequest request = new RestRequest();
    request.AddParameter("domain", "mydomain.info", ParameterType.UrlSegment);
    request.Resource = "{domain}/messages";
    request.AddParameter("from", "Excited User <[email protected]>");
    request.AddParameter("to", "[email protected]");        
    request.AddParameter("subject", "Hello");
    request.AddParameter("text", "Testing some Mailgun awesomness! This is all about the text only. Just testing the text of this email.";
    request.AddFile("attachment", Path.Combine(path1,fileName));
    request.Method = Method.POST;
    return client.Execute(request);
}
like image 73
AlexGH Avatar answered Oct 17 '22 03:10

AlexGH


I figured it out..

Not supposed to add "multipart/form-data" on the request.AddFile();

Removing this fixes the problem.

like image 6
Sinmok Avatar answered Oct 17 '22 02:10

Sinmok