Before I start explaining how to use the alternative “batchSend()” method to send email with Swift Mailer, it would be helpful to review its “setDisposition()” and “embed()” methods, discussed in the previous tutorial. They are used for attaching inline files to messages. So take a look at the code sample that uses the “setDisposition()” method to insert an image file into a basic email: // example on sending a basic email message with Swift Mailer (uses the 'addPart()' method and Swift_Attachment class and the setDisposition() method)
// include required files 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')) ->setCc(array('mary@domain.com' => 'Mary Jackson')) ->setBcc(array('susan@domain.com' => 'Susan Norton')) // build the body part of the message ->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library. Make sure to check the attached file!') ->addPart('<p>This part of the message has been formatted as HTML to make it look nicer</p>', 'text/html') ->attach(Swift_Attachment::fromPath('/path/to/file/image1.jpg')->setDisposition('inline'));
// send the email message if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; } As its name clearly suggests, the “setDisposition()” method can be used for specifying whether a specific file will be inserted inline into a message or annexed as a separate part of it. In this particular case, since my intention here was to illustrate how to embed an image into a simple message, the value passed to the pertinent method is obviously “inline().” On the other hand, Swift Mailer includes the “embed()” method, which can be used to perform a similar task when creating an email message. A basic example of its use is shown below: // example on sending a basic email message with Swift Mailer (uses the embed() method)
// include required files 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')) ->setCc(array('mary@domain.com' => 'Mary Jackson')) ->setBcc(array('susan@domain.com' => 'Susan Norton')) // build the body part of the message ->setBody('<html><head></head><body><img src="'. $message->embed(Swift_Image::fromPath('/path/to/file/image1.jpg')). '"</body>');
// send the email message if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; } There you have it. By means of the handy “embed()” method, it’s pretty easy to insert images or other MIME-supported file types into a message, without having to deal directly with the complexities of the SMTP protocol. So far, so good. At this point, you hopefully recalled how to send inline attachments with Swift Mailer, which means that it’s time to explore a few additional features offered by the library. Therefore, in the upcoming section I’m going to take a closer look at the alternative “batchSend()” method, which behaves subtly differently from its “send()” counterpart. To learn more about this brand new method, read the following segment. I’ll be there, waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|