Ilqar Rasulov Ilqar Rasulov - 3 months ago 13
PHP Question

Why Swift Mailer let to send message without password?

Using SwiftMailer package in php and local mail server hMailServer. Php code:


require 'vendor/autoload.php';

$message->setTo(array(''=>'Ilqar Rasulov'));
$message->setSubject('Delicious New Recipe');
//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!
<p>Dear James,</p>
<p>You should try this:</p>
<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>
, "text/html");
$transport= Swift_SmtpTransport::newInstance('localhost',25);
$mailer= Swift_Mailer::newInstance($transport);

} 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('', 25)

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

Also note that you need to replace, usernameand 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.