PHP 邮件 您必须至少提供一个电子邮件地址


PHP Mailer You must provide at least one email address

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

它工作正常。应该如何将我的$to字符串传递给这个$recipient数组。

$addresses = "'email1@yahoo.com','email2@gmail.com'";
$to = array($addresses);

不会神奇地创建一个包含两个元素的数组。你会得到的是数组中的一个元素,例如

$to = array(
    0 => "'email1@yahoo.com','email2@gmail.com'"
);

但是,做

$to = explode(',', $addresses);

会给你一个双元素数组:

$to = array (
    0 => "...yahoo",
    1 => "...gmail"
);
相关文章: