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
This is working fine for me.
require '../../vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-b0e955');
$domain = "domain.com";
# Make the call to the client.
$result = $mgClient->sendMessage("$domain",
array('from' => '<postmaster@domain.com>',
'to' => '<test@gmail.com>',
'subject' => 'csv file',
'text' => 'csv test file'
),array('attachment' => [
['remoteName'=>'student.csv', 'filePath'=>'/form/stu.csv']
]) );
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 <snip>==
Entity
Content-Type: multipart/form-data; boundary=<num>
And for TextView:
Content-Disposition: form-data; name="attachment"
@Test.pdf
Content-Disposition: form-data; name="attachment"; filename="Test.pdf"
Content-Type: application/pdf
<data>
Note that you POST the field 'attachment=@<filename>'. For form-data, the field name is also 'attachment', then has 'filename=<filename>' (without the '@') and finally the file contents.
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);
}