我有一个通过SMTP发送邮件的代码
if (isset ($_POST['Message'])) {
$message = $_POST['url']."
:".$_POST["Message"];
$message0 = wordwrap($message, 70, "\r\n");
\Yii::$app->mailer->useFileTransport=false;
\Yii::$app->mailer->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => 'mail.myhost.com',
'username' => 'info@myhost.com',
'password' => 'pass',
'port' => '25',
]);
$message = \Yii::$app->mailer->compose();
$contentEncoder7bit = new \Swift_Mime_ContentEncoder_PlainContentEncoder('7bit');
$message->getSwiftMessage()->setEncoder($contentEncoder7bit);
\Swift_MimePart::newInstance();
$message->setFrom('info@myhost.com')
->setTo('korrektor@mail.com')
->setSubject('Sunbject')
->setTextBody($message0);
$message->send();
return;
}
在发送邮件时,我收到错误
exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""' in
C:\wamp\www\yii\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php:383
Stack trace:
#0 C:\wamp\www\yii\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(289):
Swift_Transport_AbstractSmtpTransport->_assertResponseCode('', Array)
#1 C:\wamp\www\yii\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(117):
Swift_Transport_AbstractSmtpTransport->_readGreeting()
#2 C:\wamp\www\yii\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mailer.php(79):
Swift_Transport_AbstractSmtpTransport->start()
#3 C:\wamp\www\yii\vendor\yiisoft\yii2-swiftmailer\Mailer.php(146): Swift_Mailer->send(Object(Swift_Message))
#4 C:\wamp\www\yii\vendor\yiisoft\yii2\mail\BaseMailer.php(262): yii\swiftmailer\Mailer->sendMessage(Object(yii\swiftmailer\Message))
#5 C:\wamp\www\yii\vendor\yiisoft\yii2\mail\BaseMessage.php(48): yii\mail\BaseMailer->send(Object(yii\swiftmailer\Message))
#6 C:\wamp\www\yii\frontend\controllers\SiteController.php(1982): yii\mail\BaseMessage->send()
#7 [internal function]: frontend\controllers\SiteController->actionSendorphus()
#8 C:\wamp\www\yii\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#9 C:\wamp\www\yii\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#10 C:\wamp\www\yii\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('sendorphus', Array)
#11 C:\wamp\www\yii\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('site/sendorphus', Array)
#12 C:\wamp\www\yii\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#13 C:\wamp\www\yii\frontend\web\index.php(19): yii\base\Application->run()
#14 {main}