In the previous segment, I discussed how to create a SMTP transport object from the “Swift_SmtpTransport” class provided by Swift Mailer. Now, I"m going to build a brand new mailer object, which will permit us to send a great variety of email messages very easily. After that, I’m going to create another object, which will be responsible for building the different parts of the email message itself. Having explained that, here’s the code required to perform these tasks. Take a look at it, please: // 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('Testing Swift Mailer') // specify the From argument ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio')) // specify the To argument ->setTo(array('john@domain.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'); Definitely, the above script is extremely simple to follow, even if you don’t have a strong background in working with classes and objects. It begins creating a new mailer object by statically calling the factory “newInstance()” method of the “Swift_Mailer” class. Once this process has been completed correctly, a basic email message is built by using a separate class called, not surprisingly, “Swift_Message.” Again, it’s valid to note here the clever use of the static, chainable factory “newInstance()” method. It returns a message object to client code and allows you to specify certain common parameters of the message, such as the subject, the From and To arguments, and finally the body part. Well, at this stage I've explained in a step-by-step fashion how to create the different objects required by Swift Mailer to send a simple email message. However, the best way to understand how this process is performed is by putting all of the pieces together in the same script. With that idea in mind, below I listed the complete script. It first builds the SMTP transport object, then creates the corresponding mailer, and finally composes the message with the proper arguments. Here it is: // include required file require_once 'lib/swift_required.php';
// create the mail transport using the 'newInstance()' method $transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25) ->setUsername('alejandro@domain.com') ->setPassword('password');
// 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('Testing Swift Mailer') // specify the From argument ->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio')) // specify the To argument ->setTo(array('john@domain.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'); I don’t want to sound a bit over-excited here, but things are definitely becoming much more interesting now. Everything has been set up to send a basic email message with Swift Mailer. However, there’s something missing yet; it’s necessary to call a method of the mailer object that actually dispatches the message in question. Not surprisingly, this method has been called “send(),” and in the last section of this tutorial I’m going to discuss how to use it in conjunction with the script developed before. Now, to find out how to send email messages with this new method, click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|