33

Here is my code:

<?php
require_once 'Swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
  ->setUsername('me@ff.com')
  ->setPassword('pass');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('me@ff.com' => 'MY NAME'))
  ->setTo(array('you@ss.com' => 'YOU'))
  ->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');  
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>

AFter RUNNING GOT THIS ERROR...

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""' in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php:406

Stack trace: 
#0 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(299): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('', Array) 
#1 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(107): Swift_Transport_AbstractSmtpTransport->_readGreeting() 
#2 /home/sitenyou/public_html/Swift/lib/classes/Swift/Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start() 
#3 /home/sitenyou/public_html/sgmail.php(16): Swift_Mailer->send(Object(Swift_Message)) 
#4 {main} thrown in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 406
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
champ
  • 807
  • 2
  • 11
  • 23
  • Really strange is one thing. When I try to do the same thing in Eclipse, the autocomplete doesn't show `setUsername` and `setPassword` methods. I wonder are they possible or is Eclipse buggy with, that feature. – Eugene Oct 24 '10 at 17:52
  • 1
    @Eugene As far as I can tell, the magic `__call()` method handles those functions, so the lack of autocomplete is normal (no `@method` phpdoc either). – Maerlyn Oct 24 '10 at 21:46
  • Please refer the Tutorial, It is showing how to use Shiftmailer http://sgeek.org/send-email-attachment-using-swiftmailer-symfony/ – Gopal Joshi Apr 15 '17 at 12:59

9 Answers9

53

GMail's SMTP requires encryption. Use:

Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl");
Raptor
  • 53,206
  • 45
  • 230
  • 366
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
8

there is missing the ssl parameter, it should be something like that

Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")

Tested and work fine

osos
  • 2,103
  • 5
  • 28
  • 42
3

Swift SmtpTransport - Code (send a email)

The SMTP of GMAIL is: smtp.googlemail.com

The Full Code:

<?php
$pEmailGmail = 'xxxx@gmail.com';
$pPasswordGmail = '********';
$pFromName = 'MundialSYS.com'; //display name

$pTo = 'xxxxxx@xxxx.xxx'; //destination email
$pSubjetc = "Hello MundialSYS"; //the subjetc 
$pBody = '<html><body><p>Hello MundialSYS</p></html></body>'; //body html

$transport = Swift_SmtpTransport::newInstance('smtp.googlemail.com', 465, 'ssl')
            ->setUsername($pEmailGmail)
            ->setPassword($pPasswordGmail);

$mMailer = Swift_Mailer::newInstance($transport);

$mEmail = Swift_Message::newInstance();
$mEmail->setSubject($pSubjetc);
$mEmail->setTo($pTo);
$mEmail->setFrom(array($pEmailGmail => $pFromName));
$mEmail->setBody($pBody, 'text/html'); //body html

if($mMailer->send($mEmail) == 1){
    echo 'send ok';
}
else {
    echo 'send error';
}
?>
Greg
  • 23,155
  • 11
  • 57
  • 79
Fernando
  • 1,126
  • 12
  • 13
2

I have managed to get this working without the SSL, here is how:

$transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
            ->setUsername('contact@columbussoft.com')
            ->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance($subject)
            ->setFrom(array($emailTo=>$name))
            ->setTo(array($emailTo=>'Neo Nosrati'))
            ->addPart($body,'text/plain')
            ->setReturnPath('other@columbussoft.com');
Neo
  • 11,078
  • 2
  • 68
  • 79
  • 1
    I don't know if my work's proxy blocks the other method of using SwiftMailer but this answer works for me so if people are having difficulty with the third parameter `'ssl'` option, try this instead. Thanks @Neo. – turbonerd Sep 06 '13 at 09:15
2

I cannot be sure, but I think that Gmail's port is 587 using TLS, which is not SSL, but a newer version of it. You should check into that, because I think you are placing the wrong construction code.

Best of luck!

David Conde
  • 4,631
  • 2
  • 35
  • 48
1

I'm using the "Messages Swift Mailer" bundle in Laravel 3 and having the same issue. After some testing, in my case, the solution was to set the same email address that I used in the SMTP authentication on the "from" parameter.

I was trying to use a different address and that was triggering the "swiftmailer expected response code 220 but got code with message" error.

Hope that helps.

darksoulsong
  • 13,988
  • 14
  • 47
  • 90
1

I got same error before and i added "ssl" parameter in Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") like osos said.

IT WORKS!! thanks..:D

this is my code:

<?php
require_once 'swift/lib/swift_required.php';

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

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('THIS IS THE SUBJECT')
  ->setFrom(array('XXXXXXX@gmail.com' => 'YOUR NAME'))
  ->setTo(array('XXXXXXX@gmail.com' => 'YOU'))
  ->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');  
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>
matz
  • 11
  • 1
1

For google apps, in addition to setting to port 465 and ssl as recommended in the accepted answer, you may have to enable allow less secure apps setting, as per https://stackoverflow.com/a/25238515/947370

Community
  • 1
  • 1
squarecandy
  • 4,894
  • 3
  • 34
  • 45
0

Given the age of the question I have just recently had to go through this process so I would like to confirm for anyone still coming here that it is all still relavent ^^ but here is a full guide on how to enable 2-Step Verification, get your App Password and send an email.

composer require swiftmailer/swiftmailer

When using gmail as mentioned previously you need to have 2-Step Verification enabled on your Google Account and create an App Password.

How to get to 2-Step Verification on your Google Account

Click on the 9 dots > Security > 2-Step Verification > Get Started Enter your mobile and then verify the device.

Once you have enabled it you need to go to 2-Step Verification and then scroll to the bottom and find App Passwords.

enter image description here

Create a new password and then use it in your code as below:

enter image description here

<?php
require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
          ->setUsername('youremail@address.com')
          ->setPassword('yourapppassword');
$message_text = "Hello User,\n\This is the body of your email message";
$message = (new Swift_Message('Your Email Subject'))
->setFrom(['youremail@address.com' => 'Your From Name'])
->setBody($message_text);
$mailer = new Swift_Mailer($transport);
$message->setTo('whereyouwantto@email.com');
$result = $mailer->send($message);
?>