【发布时间】:2020-01-24 23:00:09
【问题描述】:
我有以下 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','notify@mydomain.com','NOTIFICATION SUBJECT',$htm,'','','');
}
一切正常,直到我在$eml 字段中有多个用逗号分隔的电子邮件地址。
然后我收到以下错误消息:
'to' 参数不是有效地址。请检查文档
我检查了文档,它告诉我,我可以有多个用逗号分隔的电子邮件地址。参考: mailgun API documentation
一些想法如何解决这个问题?
提前致谢
【问题讨论】:
-
如果您有一个 ,(逗号)分隔的电子邮件列表,您的
to应该看起来有点不同。您不能只有一组<>您需要在列表中的每个电子邮件地址周围都有它。 -
我可以忽略'toname'参数吗?这意味着我将只发布 email1@domain.com,email2@domain.com 而没有 ?
-
旁注:您不需要在
if语句中的每个表达式周围都使用所有()。它可以重写为:if (strlen($eml) > 5 && isset($eml) && !is_null($eml)),这使得它更加更具可读性。 -
-
请再次检查我的示例。我意识到我使用“表达式”而不是“条件”。所有条件都有一对括号,而不是每个条件。所以你可以用
if ($foo == 1 && $bar == 2)代替if (($foo == 1) && ($bar == 2))。如果你有类似的东西,你只需要对条件进行分组:if ($foo == 1 && ($bar == 1 || $bar == 2))。 Afaik,这与例如 C# 没有区别