require_once('phpmailer/class.phpmailer.php');
function smtpmailer($to,$from,$subject,$body) {
define('GUSER', 'xxx'); // Gmail username
define('GPWD', 'xxx'); // Gmail password
printf("list:".$to);
$recipient = array ($to);
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, "Bank Negara");
$mail->Subject = $subject;
$mail->Body = $body;
foreach ($recipient as $email){
$mail->AddAddress($email);
}
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
我传递的字符串以这种格式保存电子邮件地址:
'email1@yahoo.com','email2@gmail.com'
当我通过这个
时$recipient = array ($to);
我遇到错误地址无效:但是当我像这样直接传递字符串输出时:
$recipient = array ('email1@yahoo.com','email2@gmail.com');
工作正常。应该如何将$ $传递给这个$ recipient数组。
答案 0 :(得分:7)
$addresses = "'email1@yahoo.com','email2@gmail.com'";
$to = array($addresses);
不会神奇地创建一个包含两个元素的数组。你得到的是数组中的SINGLE元素,例如
$to = array(
0 => "'email1@yahoo.com','email2@gmail.com'"
);
然而,做
$to = explode(',', $addresses);
会给你一个两元素数组:
$to = array (
0 => "...yahoo",
1 => "...gmail"
);