简体   繁体   中英

trying to send mail using swift mailer, gmail smtp, php

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

GMail's SMTP requires encryption. Use:

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

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

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

Tested and work fine

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';
}
?>

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');

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!

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.

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);
?>

对于谷歌应用程序,除了按照已接受的答案中的建议设置为端口 465 和 ssl 外,您可能还需要根据https://stackoverflow.com/a/25238515/947370启用允许安全性较低的应用程序设置

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