Using SwiftMailer package in php and local mail server hMailServer. Php code:
<?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();
}
I have configured my hMailServer and assigned passwords (admin and accounts password). So question is why swiftmailer needs only hostname and password to do its job, and doen't require authentification? Cuz I cann't remember that I stored username and password in any configuration files.
In order to send your email from hMailServer, you need to create a transport object like this (as explained 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);
Also note that you need to replace smtp.example.org
, username
and passwort
with your corresponding settings of hMailServer.
Currently your script does not send the email via hMailServer but from the server where your script is located with the php mail() function. You can use any FROM
address with the mail()
function, but many email provider will recognize this as spam. So its a good idea to create a transport object.