PHP Mailer您必须提供至少一个电子邮件地址 [英] PHP Mailer You must provide at least one email address

查看:84
本文介绍了PHP Mailer您必须提供至少一个电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'

当我通过此

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

工作正常.应该如何将$ to字符串传递给此$ recipient数组.

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

推荐答案

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

但是,做

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

将为您提供两个元素的数组:

WILL give you a two element array:

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

这篇关于PHP Mailer您必须提供至少一个电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆