"pear mail包验证失败"

3
<?php

    require_once "Mail.php";

    $from = '<from.gmail.com>';
    $to = '<to.yahoo.com>';
    $subject = 'Hi!';
    $body = "Hi,\n\nHow are you?";

    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    );

    $smtp = Mail::factory('smtp', array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => '465',
            'auth' => true,
            'username' => 'johndoe@gmail.com',
            'password' => 'passwordxxx'
        ));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
        echo('<p>' . $mail->getMessage() . '</p>');
    } else {
        echo('<p>Message successfully sent!</p>');
    }

?>

然而,在$mail = $smtp->send($to, $headers, $body);处程序失败,错误提示为:

验证失败:johndoe@gmail.com

我已经确认了电子邮件和密码的正确性。如果我提供一个无效的密码,则错误会更改为

密码不正确

我还尝试禁用我的帐户上的两步验证,但没有帮助。请告知我为什么在凭据似乎正确的情况下我可能会收到此验证错误?


4
你有什么问题? - simeg
我所看到的只有代码。你确定你已经正确设置了依赖项吗? - Dave Chen
我已经正确设置了,使用了我的邮箱地址,但它没有起作用。 - user3226561
只是确认一下,您的帐户是否设置了双重身份验证?不确定ssl://smtp.gmail.com是否强制执行此项措施,但认为这值得在前期进行评论以排除可能性。 - Mike Guthrie
假设我输入了错误的密码,它会返回“密码不正确”,但如果我输入正确的密码,则会返回“验证失败:”。 - user3226561
显示剩余3条评论
4个回答

2

Usually you get "Validation failed for" when you use invalid characters as email alias in your or recipient's email.

For example:

my_email@gmail.com <first_name.last_name>
my_email@gmail.com <first_name@last_name>

Invalid characters are:

. (dots)
@ (at)
" (double quotes)
[ (bracket opened without closing)
] (bracket closed without opening)
( (parenthesis opened without closing)
) (parenthesis closed without opening)


1

我收到了相同的错误。

在尝试了不同的字段后,以下方法对我有效:

不要使用

$from = "Sender <sender@example.com>";

尝试

$from = "sender@example.com";

所以你只有发件人邮箱地址在发件人字段中,没有其他东西,也没有括号。

我承认这不是一个非常优雅的解决方案,但它仍然是一个解决方案。


0

当我尝试使用空格或没有电子邮件地址设置mailFrom时,出现了以下错误:

$this->mailFrom = "Lorem ipsum";'

只需用电子邮件地址替换它:

$this->mailFrom = "Lorem ipsum <no-reply@test.com>";

-1
即使这是一个晚回答,我也会为其他人发布这个。 “验证失败:”是针对接收者地址而不是您的凭据。 问题在于收件人地址格式错误,Gmail无法接受。
//instead of 
$to = '<to.yahoo.com>';
//use
$to = 'Recipient Name <myname@example.com>'
//or simple 
$to = 'myname@example.com';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接