user3226561
user3226561

Reputation: 31

Validation failed for: pear mail package

<?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' => '[email protected]',
            'password' => 'passwordxxx'
        ));

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

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

?>

However, the program fails at $mail = $smtp->send($to, $headers, $body); with:

validation failed: [email protected]

I've verified that email and password are correct. If I supply an invalid password, the error changes to

incorrect password

I've also tried disabling 2-step authentication on my account, but that has not helped. Please advise why I may be getting this validation error when credentials otherwise seem correct?

Upvotes: 3

Views: 5618

Answers (4)

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

I had got this error when I tried to set mailFrom with space and without email:

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

Just change it with email address:

$this->mailFrom = "Lorem ipsum <[email protected]>";

Upvotes: 0

blogo
blogo

Reputation: 329

I received the same error.

After experimenting with the fields this worked for me:

Instead of

$from = "Sender <[email protected]>";

Try

$from = "[email protected]";

So you have just the from email address in the from field. Nothing else. No brackets.

Not a very elegant solution, I admit, but a solution nonetheless.

Upvotes: 1

adamioan
adamioan

Reputation: 81

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

For example:

[email protected] <first_name.last_name>
[email protected] <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)

Upvotes: 2

Jinxmcg
Jinxmcg

Reputation: 1970

Even if it is a late answer I will post this for others. "validation failed: " is for receiver addresses not for your credentials. The issue is that the recipient address has a wrong format that Gmail does not accept.

//instead of 
$to = '<to.yahoo.com>';
//use
$to = 'Recipient Name <[email protected]>'
//or simple 
$to = '[email protected]';

Upvotes: -1

Related Questions