Php mailer google smtp working on localhost not wo

2019-04-13 08:12发布

问题:

I'm trying to send mail to registered user after registration.

For that I m using phpmailer library.

my code is as below:

function smtpmailer($to, $from, $from_name, $subject, $body) {
    global $error;
    $username = "xyz@demo.net";
    $password = "1234567";
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->Username = $username;
    $mail->Password = $password;
    $mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = '8bit';
    $mail->ContentType = 'text/html; charset=utf-8\r\n';
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    $mail->IsHTML(true);
    $mail->WordWrap    = 900; 
    if (!$mail->Send()) {
        $error = 'Mail error: ' . $mail->ErrorInfo;
        return false;
    } else {
        $error = 'Message sent!';
        $mail->SmtpClose();
        return true;

    }

This code works fine on localhost but i uploaded it to mysite then it is showing following error: SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed.

my site is hosted in folder eg (myhost.in/mysite).

Please help me to figure out problem and to solve it

Thanks in Advance...

回答1:

Go to link - https://accounts.google.com/b/0/DisplayUnlockCaptcha then click allow access button Reason : google does not allow any program to login automatically in a user's a/c for security reasons.



回答2:

I got the issue some of hosting provider not supporting external host for mail. you have to ask your hosting support weather they allow external host or not.

I used imap settings of my hosting provider and now it is solved hope this will help someone!!



回答3:

https://support.google.com/mail/answer/78754

You probably need to turn on the "Access for less secure apps" setting https://www.google.com/settings/security/lesssecureapps Helped me