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
Adding Multiple Items to Woocommerce Cart at Once
How to Wrap Long Lines Without Spaces in HTML
How to Replace Multiple Items from a Text String in PHP
Php's File_Exists() Will Not Work for Me
Making a Http Get Request with Http-Basic Authentication
Best Way to Autoload Classes in PHP
Group Array Values Based on Key in PHP
Redirecting Wordpress's Login/Register Page to a Custom Login/Registration Page
Highlight Multiple Keywords in Search
Php: How to Generate a Hmacsha256 Signature of a String
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
Add Class="Active" to Active Page Using PHP
Array_Unique and Then Renumbering Keys
How to Insert Array of Data into MySQL Using PHP