Not surprisingly, Swift Mailer allows you to send email messages by using distinct SMTP servers very easily. This process is reduced to creating a new transport object with the proper arguments, and nothing else.
But this is only dull and boring theory, so let me give you a practical example that shows how to dispatch a basic email by utilizing a local host. Of course, the example assumes that a SMTP server has been previously installed on a local machine. Having clarified that point, its complete source code looks like this:
// example on sending a basic email message with Swift Mailer (uses localhost as the SMTP transport)
// include required files
// create the mail transport using the 'newInstance()' method
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
// create the mailer using the 'newInstance()' method
$mailer = Swift_Mailer::newInstance($transport);
// create a simple message using the 'newInstance()' method
$message = Swift_Message::newInstance()
// specify the subject of the message
->setSubject('Sending email with Swift Mailer via localhost')
// specify the From argument
->setFrom(array('firstname.lastname@example.org' => 'Alejandro Gervasio'))
// specify the To argument
->setTo(array('email@example.com' =>' John Doe'))
// build the body part of the message
->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library');
// send the email message
echo 'The message was sent successfully!';
echo 'Error sending email message';
As you can see, sending an email message by using a local SMTP server doesn't differ too much from performing the same task with a remote host. The only difference rests on the first argument passed to the factory method of the "Swift_SmtpTransport" class, which in this case must be the "localhost" string.
The rest of the process of sending the email message remains nearly the same, so I'm not going to spend more time explaining how it works. Instead, I'm going to do something much more useful; in the last section of this article I'll be demonstrating how to send emails by using different SMTP servers in the same script.
That sounds pretty interesting, right? If you wish to learn how this process will be accomplished, you'll have to click on the link below and read the last segment.
blog comments powered by Disqus