Search code examples
phpsmtpgmailphpmailer

Php mailer google smtp working on localhost not working on live site


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 = "[email protected]";
    $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...


Solution

  • 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!!