简体   繁体   中英

Why can I not send mail with swiftmailer in my hosted domain but there is no error when sending it from my localhost server

I have used this code to send mail from my localhost server but when I tried to send the exact same mail with the same code uploaded to my hosting server (using cpanel) i get these errors:

<b>Fatal error</b>: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username &quot;mail@gmail.com&quot; using 2 posible authenticators' in /home/USER/public_html/mailFolder/swiftmailer-5.x/lib/classes/swift/Transport/Esmtp/AuthHandeler.php:181
Stack trace:
#0/home/USER/public_html/mailFolder/swiftmailer-5.x/lib/classes/swift/Transport/EsmtpTransport.php(333):
Swift_Transport_Esmtp_authHandeler-&gt;afterEhlo(Object(Swift_SmtpTransport))
#1/home/USER/public_html/mailFolder/swiftmailer-5.x/lib/classes/swift/Transport/AbstractSmtpTransport.php(118):
Swift_Transport_EsmtpTransport-&gt;_doHeloCommand()
#2/home/USER/public_html/mailFolder/swiftmailer-5.x/lib/classes/swift/Mailer.php(79):Swift_transport_abstractSampTransport-&gt;start()
#3/home/USER/public_html/mailFolder/sendMyMail.php(109):swift_mailer-&gt;send(Object(swift_message))
#4{main}
 thrown in <b>/home/USER/public_html/mailFolder/swiftmailer-5.x/lib/classes/swift/Transport/Esmtp/AuthHandler.php</b> on line <b>181</b>

here is the code:

$transport = Swift_SmtpTransport::newInstance()
            ->setHost('smtp.gmail.com')
            ->setPort('587')
            ->setEncryption('tls')
            ->setUsername('mail@gmail.com')
            ->setPassword('password')
        ;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
    ->setSubject($title)
    ->setFrom(array('mail@gmail.com' => 'myMail'))
    ->setTo(array($res => 'TO: '))
    ->setBody($body,'text/html');
     $headers = $message->getHeaders();
     $headers->addTextHeader('Content-Type', 'text/html');

$result = $mailer->send($message);
echo $result."<br/>";

I have gone here to unblock my IP from the server: http://www.google.com/accounts/DisplayUnlockCaptcha

and have also I have enabled "Access for less secure apps" https://www.google.com/settings/security/lesssecureapps

Try change your port to 465 and host to ssl://smtp.gmail.com. See if it works.

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
   ->setUsername('YOUR_GMAIL_USERNAME')
   ->setPassword('YOUR_GMAIL_PASSWORD');

To put simply PHPMailer is better to use than Swiftmailer.

Changing from Swiftmailer to PHPMailer does seem to be a better option. The reason for this is because PHPMailer appears to have better access and support for debugging information.

with PHPMailer you can use $mail->SMTPDebug = PHPMailer\\PHPMailer\\SMTP::DEBUG_SERVER; to make your script return a bunch of information on your email.

You can also place it inside a try catch

    try { 

              /* emailing functions and scripting goes here */ 

        } catch (PHPMailer\PHPMailer\Exception $e) {

           // Now you know that it failed and exactly why it failed
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

        }

Which actually tells you what the problem is instead of the whole page shutting down as that tells you nothing.

On top of that you can troubleshoot on github using this link https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting . To see what are the most common causes of failed e mails in your script.

Yes so overall, PHPMailer is a much better e mailing library to use as it has a much better support for debugging. Swiftmailer do try a little bit, they have they're own debugging page on github https://github.com/swiftmailer/swiftmailer/issues , And they encourage people to go there to post their bug issues however, they don't have a debugging page like PHPMailer.

In my example, my hosting provider is godaddy and they have a number of frustrating problems when it comes to sending e mails and the information is all there on the github, PHPMailer troubleshooting page.

Yes so I think a good answer to the question is to simply change to a different e mailing library.

Hi this solution can be get when u generate APP PASSWORD inside your google account security and enable 2nd authenthication.

Follow this guide to setPassword('YOUR_GMAIL_PASSWORD'); to 16 characters google app password provided.

Details instruction how to generate App Password can go thru this link : https://support.google.com/accounts/answer/185833?hl=en

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