0

I have following code that uses Swift mailer to send mail via PHP

<?php
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
            ->setUsername('my.email@mydomain.com')
            ->setPassword('emailpassword');


$message = Swift_Message::newInstance()
      ->setSubject("This is test subject")
      ->setFrom(array("sender.email@gmail.com"))
      ->setTo(array("recipient.email@gmail.com"))
      ->setBody("This is test mail body","text/html");

$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

This works pretty well, the recipient gets an email. But however, if I change username and password in transport to that of gmail like this -

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
            ->setUsername('sender.email@gmail.com') //this is the change, using gmail
            ->setPassword('senderpassword'); //this is the change

Then this doesnot work.

Can anybody tell me where I m doing wrong? what configuration I m missing?

P.S. I checked SPAM folder as well

WatsMyName
  • 4,240
  • 5
  • 42
  • 73

2 Answers2

0

Have u check your 2-Step Verification in your gmail account? Is it off? I have same problem before when 2-Step Verification is turned on. I turned it off then it work. Maybe you could give a try.

JunieL
  • 87
  • 10
  • Where in Gmail shall I check for 2-step verification?? – WatsMyName May 03 '17 at 04:45
  • Open your gmail. Then on top right site, click on your profile photo then click My Account. Go to sign in & security then signing in to google. You can see it on the right hand side. Inside box. Hope this help you. – JunieL May 03 '17 at 04:50
  • or try change the smtp port to 587 – JunieL May 03 '17 at 04:55
  • There is also a gmail setting for `less than secure apps`as well. May be worth a try looking into. I use PHP Mailer with Gmail and have no issue, both locally and remote and I did not adjust for that setting either, but it may assist. Best of luck... [Google - Lees Than Secure Apps](https://support.google.com/accounts/answer/6010255?hl=en) – dale landry May 03 '17 at 05:06
  • @dalelandry, Also allow less secure apps is turned ON – WatsMyName May 03 '17 at 05:31
  • @WatsMyName do you have any error handling of any kind with Swift, did you get anything back about the trace route or errors? PHP Mailer has a setting that can be commented in that will post an array of all traces of the email process, this can give you great clues as to where the process may be breaking down. Found this here, may help.... [Similar Question](http://stackoverflow.com/questions/3478906/using-phps-swiftmailer-with-gmail) and this [Similar Question](http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page/12317831#12317831) – dale landry May 03 '17 at 05:48
  • @dalelandry, Error reporting enabled but no error thrown – WatsMyName May 03 '17 at 06:05
  • @WatsMyName Hmm, is there no trace that can be printed through the error handling? For example, in PHP Mailer, I can invoke the following code in my mailer function, `$mail->SMTPDebug = 3;`, then comment out any header redirects. This will Enable verbose debug output once the redirect happen in your browser, it returns an array of the output as a print-r() kind of format. You can see valuable info on the trace and if/when it fails. This return is indicative of the level of error handling I have set in `$mail->SMTPDebug=3`. Not sure if Swift has settings that allow for this kind of trace. – dale landry May 03 '17 at 07:34
0

The only thing that worked is by unlocking the captcha

https://accounts.google.com/UnlockCaptcha

I got this answer from This SO Question. See reply with highest upvotes.

Community
  • 1
  • 1
WatsMyName
  • 4,240
  • 5
  • 42
  • 73