Skip to content
Advertisement

mailgun send email to multiple users

i have the following mailgun.php file:

define('MAILGUN_URL', 'https://api.eu.mailgun.net/v3/my_domain');
define('MAILGUN_KEY', 'xxxxxxxxx'); 
function sendmailbymailgun($to,$toname,$mailfromnane,$mailfrom,$subject,$html,$text,$tag,$replyto){
    $array_data = array(
        'from'=> $mailfromnane .'<'.$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
    );
    $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;
}

I am trying to send a notification email to end users with the following code: $eml = email address variable $usrn = user name $htm = html text generated from mailtext.php with displaytext function $cust_orderid = parameter,not really important in this case

if ((strlen($eml)>5)&&(isset($eml))&&(!is_null($eml))) {
include('./mailtext.php');
$htm = displaytext($cust_orderid);       
require_once('./mailgun.php');
sendmailbymailgun($eml,$usrn,'NOTIFICATIONS','notify@mydomain.com','NOTIFICATION SUBJECT',$htm,'','','');
          }

everything works fine, until i have in $eml field multiple email addresses separated with comma. then i got the following error message: ‘to’ parameter is not a valid address. please check documentation

i checked the documentation and it tells, that i can have multiple email addresses separated by comma. reference: mailgun API documentation

some ideas how to solve this problem?

thanks in advance

Advertisement

Answer

So after a suggestion of @Igor Ilic i modified the mailgun.php like this:

<?
define('MAILGUN_URL', 'https://api.eu.mailgun.net/v3/mydomain');
define('MAILGUN_KEY', 'xxxxxxxxxxx'); 
function sendmailbymailgun($to,$toname,$mailfromnane,$mailfrom,$subject,$html,$text,$tag,$replyto){
    $array_data = array(
        'from'=> $mailfromnane .'<'.$mailfrom.'>',
        'to'=> $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
    );
    $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;
}
?>

the only change is :

'to'=> $to,

instead of:

'to'=>$toname.'<'.$to.'>',

now i am simply ignoring the < > part of the email sending – it’s not so pretty with the display name/email address in the email header, but i don’t care, it works, even when the email address $eml parameter looks dirty like this: 'email1@gmail.com,,,email2@yahoo.com,,email3@office.com,,'

thanks for the great idea, problem solved

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement