PEAR mail authentication failure when sending emails

我的梦境 提交于 2019-12-05 21:44:38

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