1

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

4

1 回答 1

2

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

// 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.org,usernamepasswort使用您相应的 hMailServer 设置。

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

于 2016-08-27T09:16:17.743 回答