简体   繁体   中英

Im sending emails with sucess using a hotmail adress, but with gmail Im getting: SMTP Error: Could not connect to SMTP host

Im sending emails with phpmail class, and for now Im just testing in localhost and it is working fine until today.

I was testing using a gmail adress and my send email configuration is like this:

define('MAILUSER','myemail_test@gmail.com');
define('MAILPASS','mytestpass');
define('MAILPORT','587');
define('MAILHOST','smtp.gmail.com');

And, with this configuration above I was sending mails witth sucess until today. But now its not working, Im always getting this error: SMTP Error: Could not connect to SMTP host.

So I try to use a hotmail email to see if it works, and I really dont understand why but with hotmail Im sending emails with sucess, like this:

define('MAILUSER','myemail_test@hotmail.com');
define('MAILPASS','mytestpass');
define('MAILPORT','25');
define('MAILHOST','smtp.live.com');

Do you see why this can be happening?

My function:

function sendMail($subject,$message,$from,$nameFrom,$to,$nameTo, $attachment = NULL, $reply = NULL, $replyNome = NULL){
    require_once('mail/class.phpmailer.php');
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->SMTPAuth = true; 
    $mail->IsHTML(true);
    $mail->SMTPSecure = "tls"; 
    $mail->Host = MAILHOST; 
    $mail->Port = MAILPORT; 
    $mail->Username = MAILUSER; 
    $mail->Password = MAILPASS; 
    $mail->From = utf8_decode($from);
    $mail->FromName = utf8_decode($fromName); 
    if($reply != NULL){
        $mail->AddReplyTo(utf8_decode($reply),utf8_decode($replyNome)); 
    }
    $mail->Subject = utf8_decode($subject); 
    $mail->Body = utf8_decode($message); 
    $mail->AddAddress(utf8_decode($to),utf8_decode($nameTo)); 
    if($attachment != NULL){
        $mail->AddAttachment($attachment);
    }
    if($mail->Send()){
        return true;
    }
    else{
        return false;
    }
}   

It's not just a different port - you need to enable TLS as well. See http://phpmailer.worxware.com/?pg=examplebgmail

In you php.ini make sure you have uncommented the line with

(Windows) extension=php_openssl.dll

(Linux) extension=php_openssl.so

Also, for gmail, use this host :

$mail->Host = 'ssl://smtp.gmail.com:465';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM