Почему Swift Mailer позволяет отправлять сообщения без пароля?

Использование пакета SwiftMailer в php и локальном почтовом сервере 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 для своей работы нужны только имя хоста и пароль, и не требуется аутентификация? Потому что я не могу вспомнить, что я сохранил имя пользователя и пароль в любых файлах конфигурации.

1 ответ

Решение

Чтобы отправить электронное письмо от 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, usernameа также passwort с вашими соответствующими настройками hMailServer.

В настоящее время ваш скрипт не отправляет электронное письмо через hMailServer, а с сервера, где находится ваш скрипт, с помощью функции php mail(). Вы можете использовать любой FROM адрес с mail() функция, но многие почтовые провайдеры распознают это как спам. Поэтому хорошая идея создать транспортный объект.

Другие вопросы по тегам