From what I understand of your question, you've got the bottom code as part of a script you're using, but the actual mailing needs to be done along the lines of the sample Mailgun code given at the top?
I've mashed those two functions together. It's not tested. There may be errors.
The function has the same name, and gets called exactly the same way it was before, accepting the same parameters. You'll see that I've removed the bottom part of the code - which actually sent the email - and replaced it with the example code. Within that, I've replaced the placeholder text it provided with the values the function receives at the top: $to
, $subj
and $msg
. I also removed all of the $headers
lines as these refer specifically to the mail()
function, which we are not using any more. [Edit: now should work with bcc option too.]
You will still need to change a few things: my-api-key-here
and Excited User <me@samples.mailgun.org>
. This information should be available from your Mailgun account.
public function sendEmail($to, $subj, $msg, $shortcodes = '', $bcc = false) {
if ( !empty($shortcodes) && is_array($shortcodes) ) :
foreach ($shortcodes as $code => $value)
$msg = str_replace('{{'.$code.'}}', $value, $msg);
endif;
/* Multiple recepients? */
if ( is_array( $to ) )
$to = implode(', ', $to);
/* BCC address. */
$bccrecip = '';
if ( $bcc ) {
$bccrecip = $to;
$to = '';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:my-api-key-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('from' => 'Excited User <me@samples.mailgun.org>',
'to' => $to,
'bcc' => $bccrecip,
'subject' => $subj,
'text' => $msg));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}