我在发送邮件时遇到了一些问题。我有一个运行postfix的ubuntu服务器作为mailserver。 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和错误字符串'连接被拒绝',
这有点奇怪,因为我确实设法使用此代码快速发送邮件:
//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);
有人对此有任何想法吗?
谢谢!
答案 0 :(得分:1)
Symfony或PHP不是问题 - 您的本地SMTP服务器无法访问。您的邮件服务器可能存在配置问题,导致SMTP服务器无法在其本机端口(25)上使用。