I'm writing a simple script in which a gmail account is used to send an email to itself.
I altered the script from SwiftMailer's reference, but I'm not getting any results. What's wrong?
Edit: after further debugging I've found that the statement
$result = $mailer->send($message);
causes the code to fail (the echo below it doesn't print).
Why is this? Just cause the message isn't sent the program crashes? :/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once '/var/www/swift/lib/swift_required.php';
echo 'Mail sent <br />';
/* //create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587);
->setUsername('[email protected]')
->setPassword('softrain1234')
;
*/
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587)
->setUsername('[email protected]')
->setPassword('password')
;
echo 'line 40 <br />';
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]' => 'Evaluaciones'))
->setTo(array('[email protected]'=> 'A name'))
->setBody('Test Message Body')
;
echo 'line 52 <br />';
$result = $mailer->send($message);
echo $result;
echo 'line 58 <br />';
?>
</body>
</html>
The test form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Test Mail Script</title>
</head>
<body>
<form action="scriptmail.php" method="post">
<input type="submit"/>
</table>
</form>
</body>
</html>
The outgoing SMTP server, smtp.gmail.com , requires TLS. Use port 465 , or port 587 if your client begins with plain text before issuing the STARTTLS command.
Webmail (gmail, etc) uses http for sending/retrieving emails. Email clients (thunderbird, etc) use SMTP for sending emails, and POP3/IMAP to retrieve emails. (Or, do they use http to send emails too, and only SMTP servers actually use SMTP..?)
Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search for a solution despite the age.
When using PHP SwiftMailer to connect to Gmail or Google Apps email accounts you need to use the following
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($this->username)
->setPassword($this->password);
$this->mailer = Swift_Mailer::newInstance($transporter);
This works fine for me. In the original code, you are using port 587 not 465 and you are not specifying the protocol (ssl). Don't know if that bit matters, but for me port 587 failed and 465 worked fine.
Hope that helps.
I found this question doing a google search and used the code in the answer provided by fullybaked. It got me to the point where I could see in the Exceptions thrown that the Google smtp server was responding with an error code indicating password and username not accepted. Upon further investigation I found the following steps. Now it works great and I can continue to flesh out the code for production.
Not sure when Google made the change but in addition to the configuring your code with your Google username, password and port (465/ssl or 587/tls) you need to do the following steps to be able to use the Google smtp server.
To use the the gmail smtp server with your gmail account you need to:
1) In Google "Account settings" enable "Access for less secured apps" by setting it to "Allow".
2) In gmail settings under the "Forwarding and POP/IMAP" tab, check the IMAP status, it needs to be enabled. (This will allow the emails sent to be saved in the sent folder.)
3) If after steps 1 and 2 you still throw the Exception Google's smtp
server is not accepting the user name and password you need to open
a browser, go to gmail and log in, then open another tab in the
same browser andgo to this address:
https://accounts.google.com/DisplayUnlockCaptcha
According to Google you may have to enter a captcha.
4) Immediately send an email from your code as this will allow it to be authorized to use your gmail account.
Hope this helps.
I found that what evad said was true, but I had to change his work a little bit for the current version of Swift Mailer. Now it is:
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('[email protected]')
->setPassword('password');
$mailer = Swift_Mailer::newInstance($transporter);
Should work fine.
The GMail SMTP system has it's issues given the SSL and ports. I find it hard to get it to work with PHP nicely.
The best thing I have found, that does work is phpGMailer. You may be able to sift through that code to see how they got it to work, but that has always worked flawlessly for me.
I know this does not address the SwiftMail issue, just figured I would point that out :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With