我有以下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
=使用displaytext函数从mailtext.php生成的html文本$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','[email protected]','NOTIFICATION SUBJECT',$htm,'','','');
}
一切正常,直到我在$eml
字段中有多个用逗号分隔的电子邮件地址。然后我收到以下错误消息:“至”参数不是有效地址。请检查文档
我检查了文档,发现它可以有多个电子邮件地址,以逗号分隔。参考: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.'>',
现在我只是忽略了电子邮件发送的<>部分-电子邮件标题中的显示名称/电子邮件地址不是很漂亮,但我不在乎,即使电子邮件地址为$eml
,它也可以正常工作参数看起来像这样:'[email protected],,,[email protected],,[email protected],,'
感谢这个好主意,问题已解决