Mailgun Sent Mail with Attachment

Mailgun send mail with attachment

Change your first code to:

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example@mail.example.com>',
'to' => 'example@example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

I changed '@'.$filename to curl_file_create($filename, 'application/pdf', 'example.pdf').

See documentation curl_file_create and check the notes for PHP < 5.5.

Mailgun Sent mail With attachment

You need to change the last parameter in the following way:
attachment[1]' => '@aaa.rar

We have some samples in our documentation for this use case. Just click PHP in the top bar to switch the language.
http://documentation.mailgun.net/user_manual.html#examples-sending-messages-via-http

Please don't hesitate to send us an email with any questions to support@mailgunhq.com. We are always happy to help.

Sending email with attachments using Mailgun and Httpclient

The problem is with the name that you are giving to the body of your files(it's not the file name or title)
As the official documentation

ContentDispositionHeaderValue.Name is the the name for the content body part.

and the doc of mailgun mention that the parameter name should be 'attachment' so you can change your code to be this

fileContent.Headers.ContentDisposition = new 

ContentDispositionHeaderValue("form-data")
{
Name = "attachment",
FileName = System.IO.Path.GetFileName(somelongFilepath)
};

or to be more clear this

formContent.Add(fileContent, 'attachment', System.IO.Path.GetFileName(somelongFilepath));

How to Send Images as Attachments with Mailgun and Node.js?

mailgun.js package will accept attachment as file path, buffer and stream. To attach your image from external URL use stream,

var request = require('request');
var image = request(pictures[0]);
var data = {
from: "email <email@email.com>",
to: "email@email.com",
subject: 'this is an email',
html: 'here is a new post and here are the images in that post',
attachment: image
};

Here is the sample code from mailgun.js

var request = require('request');
var file = request("https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");

var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'serobnic@mail.ru',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!',
attachment: file
};

mailgun.messages().send(data, function (error, body) {
console.log(body);
});

Reference : https://www.npmjs.com/package/mailgun-js#attachments



Related Topics



Leave a reply



Submit