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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8297367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:22:48  来源:igfitidea点击:

PHP Mailer You must provide at least one email address

phpphpmailer

提问by Talha Bin Shakir

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;
    }
}

I am passing a string which hold the email address in such format:

我正在传递一个以这种格式保存电子邮件地址的字符串:

'[email protected]','[email protected]'

When I am passing this

当我通过这个

$recipient = array ($to);

I am having error Invalid address: But When I pass the string output directly like this:

我遇到错误无效地址:但是当我像这样直接传递字符串输出时:

$recipient = array ('[email protected]','[email protected]');

It works fine. How should pass my $to String to this $recipient array.

它工作正常。如何将我的 $to String 传递给这个 $recipient 数组。

回答by Marc B

$addresses = "'[email protected]','[email protected]'";
$to = array($addresses);

is not going to magically create an array with two elements in it. What you'll get is a SINGLE element in the array, e.g

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

$to = array(
    0 => "'[email protected]','[email protected]'"
);

However, doing

然而,做

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

WILL give you a two element array:

会给你一个两个元素的数组:

$to = array (
    0 => "...yahoo",
    1 => "...gmail"
);