PEAR mail authentication failure when sending emails

风流意气都作罢 提交于 2019-12-10 10:17:23

问题


Since I found that the in-build mail function in PHP has security vulnerabilities I tried to use PEAR. I have installed & made the necessary configuration on my localhost (WAMP server 2.2). However each time I try to send an email the following message is displayed.

error: authentication failure [SMTP: Invalid response code received from server (code: 535, response: 5.7.8 Username and Password not accepted. Learn more at 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 gg10sm16772067pbc.46 - gsmtp)].

Both the username and password is correct I have checked it over & over again. I have checked online documents & similar questions by other members but I am still stuck on this issue. Any insights to this problem is greatly appreciated. And by the way here is the PHP code that I used to send an email.

    <?php
            require "Mail.php";
            $from   = "myemail@here.com";
            $to     = "mysender@here.com";
            $subject= "hiii";
            $body =     "\n\nEmail contents here";

            $host   = "ssl://smtp.gmail.com";//"smtp.gmail.com";
            $port   = "465";//"587";
            $user   = "my username";
            $pass   = "mypassword";
            $headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
            $smtp   = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
            $mail   = @$smtp->send($to, $headers, $body);

            if(PEAR::isError($mail)){
                echo "error: {$mail->getMessage()}";
            }else{
                echo "Message sent";
            }
     ?>

回答1:


The username that you use for creating the SMTP object needs to be your full gmail email address, e.g. fred.flintstone@gmail.com and the host variable should just be "smtp.gmail.com" - it doesn't need to start with "ssl://"

This will result in an email being sent with no problems. (I put your code into a file named 20031009.php, fixed the errors, made a few other changes so it would work with my gmail account and ran it, the following is the result.)

$ php 20031009.php 
Message sent

On another note, it looks like you need to swap around the values for $from and $to. :)

This is the working code, in it's entirety (with account and email details changed back)

    require "Mail.php";
    $to      = "mysender@here.com";
    $from    = "myemail@here.com"; // the email address
    $subject = "hiii";
    $body    = "\n\nEmail contents here";

    $host    = "smtp.gmail.com";
    $port    =  "587";
    $user    = "my username";
    $pass    = "mypassword";
    $headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
    $smtp    = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
    $mail    = @$smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)){
        echo "error: {$mail->getMessage()}";
    } else {
        echo "Message sent";
    }


来源:https://stackoverflow.com/questions/20031009/pear-mail-authentication-failure-when-sending-emails

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