Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun send mail with attachment

I am trying to send a mail with attachments with mailgun. The mail itself is fine, but it is missing the attachment. Also in the mailgun log it shows up fine, but the attachment array is empty.

I replaced my credentials with example.com. The file is placed in a subdirectory and is readable.

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

$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 don't get an error, this is the $response:

string(103) "{
    "id": "<[email protected]>",
    "message": "Queued. Thank you."
}"

Within the mailgun logs no attachments are listed:

"message": {
    "headers": {
        "to": "[email protected]",
        "message-id": "[email protected]",
        "from": "Example <[email protected]>",
        "subject": "Subject"
    },
    "attachments": [],
    "size": 349
},

According to all documentation I found this would be the correct solution, but it is not working.

Thanks in advance for all replies.

like image 246
Vestalis Avatar asked May 14 '17 12:05

Vestalis


People also ask

How do I send an attachment in Mailgun?

The file is placed in a subdirectory and is readable. $filename = 'directory/example. pdf'; $parameters = array('from' => 'Example <[email protected]>', 'to' => '[email protected]', 'subject' => 'Subject', 'text' => 'This is just a test. ', 'attachment[1]' => '@' .

Does Mailgun allow attachments?

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.

How do I send an email using Mailgun?

Send via API Run this: curl -s --user 'api:YOUR_API_KEY' \ https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \ -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \ -F to=YOU@YOUR_DOMAIN_NAME \ -F [email protected] \ -F subject='Hello' \ -F text='Testing some Mailgun awesomeness!

Can Mailgun send SMS?

TextMagic, also a Mailgun customer, allows you to send notifications, alerts, reminders, confirmations and SMS marketing campaign messages to your customers, staff members and suppliers. On average, TextMagic customers are sending over 2.5M text messages around the world each month.


3 Answers

Change your first code to:

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <[email protected]>',
                'to' => '[email protected]',
                '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.

like image 186
Phillip Plum Avatar answered Oct 21 '22 01:10

Phillip Plum


or use the api https://documentation.mailgun.com/en/latest/api-sending.html#examples

once the domains and parameters are set you can then just use $result = $mgClient->messages()->send( $domain, $params );

like image 4
Craig Stanfield Avatar answered Oct 21 '22 00:10

Craig Stanfield


This worked for me.

<?php
 $path = $filename;
 define('MAILGUN_URL', 'https://api.mailgun.net/v3/domainname'); 
 define('MAILGUN_KEY', 'private api key from mail gun');
 **function sendmailbymailgun**($to,$toname,$mailfromname,$mailfrom,$subject,
  $html,$text,$tag,$replyto, $path){

$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
 'attachment[0]' =>  curl_file_create(__dir__."\\".$path, 'application/pdf', $path),
 'attachment[1]' =>  curl_file_create(__dir__."\\".$path, 'application/pdf', "example.pdf")
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}

  //: call the function
  $res = sendmailbymailgun("[email protected]","Recipeint Name", "Sender Name", "[email protected]","Email subject","Email body. find two attachment","","tags", "[email protected]", $path);
  echo "<pre>".print_r($res, true)."</pre>";  


?>
like image 3
Emanovwe Emmanuel J Avatar answered Oct 21 '22 00:10

Emanovwe Emmanuel J