Skip to content

Instantly share code, notes, and snippets.

@YugalXD
Created April 27, 2016 06:12
Show Gist options
  • Save YugalXD/26b266d4545f319c83664db56bd6f2eb to your computer and use it in GitHub Desktop.
Save YugalXD/26b266d4545f319c83664db56bd6f2eb to your computer and use it in GitHub Desktop.
Send bulk mail by mailgun api in php
<?php
define('MAILGUN_URL', 'https://api.mailgun.net/v3/DOMAIN_NAME');
define('MAILGUN_KEY', 'KEY');
function sendBulkMailByMailGun($emailArr = array(), $subject, $html, $text = '', $replyto = EMAIL_INFO, $mailcat = 'test', $mailfrom = FROM, $mailfromname = FROMNAME) {
$emails_arr = array();
if ($emailArr) {
$i=1;
foreach ($emailArr as $email) {
$email_arr .= $email.',';
$email_data[$email]['id'] = $i;
$i++;
}
$email_arr = ltrim($email_arr, ',');
}
$email_data = json_encode($email_data);
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$email_arr,
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag[1]'=>$mailcat,
'o:tag[2]'=>'tag2',
'h:Reply-To'=>$replyto,
'recipient-variables'=>$email_data
);
$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_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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment