【问题标题】:Why Swift Mailer let to send message without password?为什么 Swift Mailer 允许在没有密码的情况下发送消息?
【发布时间】:2017-01-03 21:31:07
【问题描述】:

在 php 和本地邮件服务器 hMailServer 中使用 SwiftMailer 包。 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 只需要主机名和密码来完成它的工作,并且不需要身份验证?因为我不记得我在任何配置文件中存储了用户名和密码。

【问题讨论】:

    标签: php swiftmailer hmail-server


    【解决方案1】:

    为了从 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。您可以将任何FROM 地址与mail() 功能一起使用,但许多电子邮件提供商会将其识别为垃圾邮件。所以创建一个传输对象是个好主意。

    【讨论】:

    • 所以看起来直到此时我在没有进入邮件服务器的情况下发送了邮件?我在“发件人”中更改为任何不存在的地址并且它有效!更改为您的变体后,它开始检查用户名和密码。感谢您的回答,现在我知道它是如何工作的
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 2014-09-26
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多