mailgun 向多个用户发送电子邮件

mailgun send email to multiple users

我有以下 mailgun.php 文件:

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;
}

我正在尝试使用以下代码向最终用户发送通知电子邮件: $eml = 电子邮件地址变量 $usrn = 用户名 $htm = html 从 mailtext.php 生成的带有 displaytext 函数的文本 $cust_orderid = 参数,在这种情况下并不重要

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,'','','');
          }

一切正常,直到我在 $eml 字段中有多个用逗号分隔的电子邮件地址。 然后我收到以下错误消息: 'to' 参数不是有效地址。请检查文档

我查看了文档,它告诉我,我可以有多个用逗号分隔的电子邮件地址。参考: mailgun API documentation

如何解决这个问题的一些想法?

提前致谢

所以在@Igor Ilic 的建议下,我修改了 mailgun.php 如下:

<?
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;
}
?>

唯一的变化是:

'to'=> $to,

而不是:

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

现在我只是忽略了电子邮件发送的 < > 部分 - 它在电子邮件 header 中显示 name/email 地址并不是那么漂亮,但我不在乎,它有效,即使电子邮件地址 $eml 参数看起来像这样脏:'email1@gmail.com,,,email2@yahoo.com,,email3@office.com,,'

谢谢你的好主意, 问题已解决