0

I am new to StackOverflow in terms of posting anything. Please excuse me if I am not doing this properly.

I have found a post that shows how to integrate Mailgun API within Codeigniter and it works well. Please see here - How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()

My problem comes when I want to attach a pdf in the same piece of code. instead of using

$this->mailgun::send([
  'from' => "Example.com team <no-reply@mg.example.com>",
  'to' => "somerandomuser@gmail.com",
  'subject' => "Welcome to Example.com",
  'text' => "We just want to say hi. Have fun at Example.com"
]);

I had hoped to use the following:

$this->mailgun->send([
            'from' => "Web Admin <postmaster@xxxxxx.xxxxxs.org>",
            'to' => $toemail,
            'subject' => $subj,
            'text' => $bodtxt,
            'html' => $bod,
            'attachment' => curl_file_create($increport , 'application/pdf', $increportnm),
            'h:Reply-To' => $replyname . ' <' . $replyemail . '>'
        ]);

Where $increport is the path to my pdf file and $increportnm is the name of the pdf file.

When I run this, there is no email posted to Mailgun :(

Can anyone help me out?

1 Answers1

0

No need for calling curl_file_create... this is how I do it from Codeigniter to Mailgun and it works:

$curl_post_data = array(
    'from'       => 'Emailer name <noreply@mailer.domain.com>',
    'to'         => $recipient,
    'subject'    => $subject,
    'text'       => $mailgun_text,
    'html'       => $mailgun_text_html,   
    'attachment' => @'filename.ext',
);

$service_url = 'https://api.mailgun.net/v3/mailer.mydomain.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:xxxxxxxxxxxxxxxxxxx-xxxxxx-xxxxxx"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

after that, I run $curl_response = curl_exec($curl); and parse the response as $response = json_decode($curl_response, true);

Try that. If the emails are not going out, try doing a print_r for $curl_response and check the mailgun logs for additional failure info

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
  • I must really be missing something :( I can run the code but the log in mailgun says I am not attaching a pdf? I know I have the right path because for trial purposes I hardcoded the path to the pdf in. I wonder if it's permission based? Permission on the folder is 0755 and the file is 644. Any other thoughts? Seems very strange to me. I can send emails no problem. It's the attachment. – Don Britton Dec 17 '18 at 23:49
  • you may need to base64_encode the file prior to attaching it, as attachments travel in base64 format when they are binary – Javier Larroulet Dec 17 '18 at 23:54
  • I should also say that the print_r($response) is telling me that it has queued the message. The attachments[] part of the mailgun log is empty. I am assuming it is because I am not getting the attachment name in there somehow properly. – Don Britton Dec 17 '18 at 23:54
  • Check my comment above... In my code, `$file` needs to be the actual base64 encoded content, not just the `path/file.extension` – Javier Larroulet Dec 18 '18 at 00:09
  • According to the accepted response on this question: https://stackoverflow.com/questions/14229656/mailgun-sent-mail-with-attachment what we are both missing is an `@` sign before the filename (writing from memory if left it out). I'm editing my response now – Javier Larroulet Dec 18 '18 at 00:22