I am facing issue when mail having attachment sent using mailgun. If anyone has done this thing please reply. This is my code...
$mg_api = \'key-3ax6xnjp29jd
The documentation doesn't say it explicitly, but the attachment itself is bundled into the request as multipart/form-data.
The best way to debug what's going on is use Fiddler to watch the request. Make sure you accept Fiddler's root certificate, or the request won't issue due to the SSL error.
What you should see in Fiddler is for Headers:
Cookies / Login
Authorization: Basic ==
Entity
Content-Type: multipart/form-data; boundary=
And for TextView:
Content-Disposition: form-data; name="attachment"
@Test.pdf
Content-Disposition: form-data; name="attachment"; filename="Test.pdf"
Content-Type: application/pdf
Note that you POST the field 'attachment=@
I think CURL is supposed to just do this all for you magically based on using the '@' syntax and specifying a path to a file on your local machine. But without knowing the magic behavior it's hard to grok what's really happening.
For example, in C# it looks like this:
public static void SendMail(MailMessage message) {
RestClient client = new RestClient();
client.BaseUrl = apiUrl;
client.Authenticator = new HttpBasicAuthenticator("api", apiKey);
RestRequest request = new RestRequest();
request.AddParameter("domain", domain, ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", message.From.ToString());
request.AddParameter("to", message.To[0].Address);
request.AddParameter("subject", message.Subject);
request.AddParameter("html", message.Body);
foreach (Attachment attach in message.Attachments)
{
request.AddParameter("attachment", "@" + attach.Name);
request.AddFile("attachment",
attach.ContentStream.WriteTo,
attach.Name,
attach.ContentType.MediaType);
}
...
request.Method = Method.POST;
IRestResponse response = client.Execute(request);
}