PHP Mailer You must provide at least one email address

回眸只為那壹抹淺笑 提交于 2019-12-08 09:38:54

问题


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:

'email1@yahoo.com','email2@gmail.com'

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 ('email1@yahoo.com','email2@gmail.com');

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


回答1:


$addresses = "'email1@yahoo.com','email2@gmail.com'";
$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 => "'email1@yahoo.com','email2@gmail.com'"
);

However, doing

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

WILL give you a two element array:

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


来源:https://stackoverflow.com/questions/8297367/php-mailer-you-must-provide-at-least-one-email-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!