【问题标题】:PHP Mailer You must provide at least one email addressPHP Mailer 您必须提供至少一个电子邮件地址
【发布时间】:2011-11-28 14:31:58
【问题描述】:
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 String 传递给这个 $recipient 数组。

【问题讨论】:

  • 我已从该帖子中删除了您的 gmail 帐户/密码信息。您可能应该立即更改该密码。

标签: php phpmailer


【解决方案1】:
$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"
);

【讨论】:

  • 你需要修剪每个元素的单引号吗?我的猜测是"'email1@yahoo.com'" 的地址将无效,因为单引号是字符串的一部分。
猜你喜欢
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多