为什么 Swift Mailer 允许发送没有密码的消息?

Why Swift Mailer let to send message without password?

使用 php 中的 SwiftMailer 包和本地邮件服务器 hMailServer。 Php代码:

<?php

require 'vendor/autoload.php';

$message=Swift_Message::newInstance();
$message->setFrom('webmaster@mail.com');
$message->setTo(array('testmail@mail.com'=>'Ilqar Rasulov'));
$message->setSubject('Delicious New Recipe');
//$message->setBody(<<<_TEXT_
//Dear James,
//You should try this: puree 1 pound of chicken with two pounds
//of asparagus in the blender, then drop small balls of the mixture
//into a deep fryer. Yummy!
//Love,
//Julia
//_TEXT_
//);
$message->addPart(<<<_TEXT_
<p>Dear James,</p>
<p>You should try this:</p>
<ul>
<li>puree 1 pound of chicken with two pounds
of asparagus in the blender</li>
<li>then drop small balls of the mixture into a deep fryer.</li>
</ul>    
<p><em>Yummy!</em></p>
<p>Love,</p>
<p>Julia</p>
_TEXT_
, "text/html");
try{
$transport= Swift_SmtpTransport::newInstance('localhost',25);
$mailer=  Swift_Mailer::newInstance($transport);

$mailer->send($message);
}  catch (Exception $ex){
    print $ex->getMessage();
}

我已经配置了我的 hMailServer 并分配了密码(管理员和帐户密码)。所以问题是为什么 swiftmailer 只需要主机名和密码来完成它的工作,而不需要身份验证?因为我不记得我在任何配置文件中存储了用户名和密码。

为了从 hMailServer 发送电子邮件,您需要像这样创建一个传输对象(如 here 所述):

// Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('username')
  ->setPassword('password')
  ;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

另请注意,您需要将smtp.example.orgusernamepasswort替换为您对hMailServer的相应设置。

目前您的脚本不通过 h​​MailServer 发送电子邮件,而是从您的脚本所在的服务器发送电子邮件 php mail() function。您可以使用具有 mail() 功能的任何 FROM 地址,但许多电子邮件提供商会将其识别为垃圾邮件。所以创建一个传输对象是个好主意。