我在发送邮件时遇到了一些问题。我有一个运行 postfix 作为邮件服务器的 ubuntu 服务器。该 webapp 基于 symfony 1.2 并使用 Swift 3 进行邮件发送。Everyrhing 在同一台服务器上运行。这是我试图运行的代码:
public static function sendMailLocal($mailFrom, $mailto, $subject, $mailBody, $prevError)
{
$mailer = null;
try
{
$connection = new Swift_Connection_SMTP('localhost', '25');
$mailer = new Swift($connection);
// Create the mailer and message objects
$message = new Swift_Message($subject, $mailBody, 'text/html');
// Send
$mailer->send($message, $mailto, $mailFrom);
$mailer->disconnect();
}
catch (Exception $e)
{
if($mailer != null)
$mailer->disconnect();
throw new EmailTransferException ("There was an error while trying to send the email - locally:" . $e->getMessage() . " , first error:" . $prevError);
}
}
这是我得到的错误消息:
尝试在本地发送电子邮件时出错:SMTP 连接无法启动 [localhost:25]:fsockopen 返回错误号 111 和错误字符串“连接被拒绝”,
这有点奇怪,因为我确实设法使用以下代码在没有 swift 的情况下发送了一封邮件:
//adresses are examples
$to = "john.doe@mail.com";
$sender_email = "someone@mail.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: <'.$to.'>' . "\r\n";
$headers .= 'From: John Doe<'.$sender_email.'>' . "\r\n";
// Mail it
$success = mail($to, "test from server", "msg", $headers);
有人对此有任何想法吗?
谢谢!