Join David Powers for an in-depth discussion in this video Preparing a basic email, part of PHP: Email with Swift Mailer.
- In this chapter, we'll look at the basics of sending email with Swift Mailer, starting with preparing a basic email. Every email has four required elements. The subject of the email, the recipient's address or addresses, the from address, this should be where the email originates, not an email address extracted from an online form, and finally, the message body. Let's see how to handle these four elements with Swift Mailer.
In my editing program, I've opened message.php which you can find in the Chapter 2 02_01 folder of the exercise files. It includes config.php which was created in the previous chapter. This calls an autoloader, giving us access to the Swift Mailer library. There's also this try catch structure, and in the catch block I've added code to display error messages if Swift Mailer throws an exception, and all of the code needs to go inside the try block, starting on Line Six.
Swift Mailer handles the preparation of the email separately from sending it. You prepare the message by creating a new instance of the Swift message class. Although you can instantiate the class with the new keyword, it's more convenient to use a static method, because then you can chain other methods onto it. So I'm going to create a variable for my message, simply call it message. Then we need Swift_Message. The scope resolution operator, a double colon, and then the new instance static method.
This accepts four arguments, but all of them are optional. Rather than trying to remember the correct order, we can just use the arrow operator to build the message details, so I'm going to go outside of the parentheses, then on to the next line, and the arrow operator, and I'll start off by setting the subject line, so they use the setSubject method. This expects a string. Then we'll chain another method onto that with the arrow operator, this time we'll set the from address.
If you're just using an email address, the setFrom method accepts a string as its argument, but if you want to add the sender's name or company name, the argument must be an associative array, with the email address as the key, and the name as the value, so let's do that. I'm going to add in the email address that I'm going to use as the from address, you'll need to add your own email address in here. And then the double arrow operator. And my domain name.
Then we'll add the recipient's address, there are two ways to do this. You can either use the setTo method or the addTo method. Although they both set the recipient's address, there's an important difference in the way they work. I'm going to look at setTo first. So setTo, and like the setFrom method, this accepts a single email address as a string, but if you want to include the recipient's name, it must be an associative array, so I'll use an associative array, and the recipient will be one of my testing emails.
And then I'll assign my own name to that. And then we'll add the body. And it will come as no surprise that you use setBody to do that, and like the PHP mail function, the whole of the body needs to be a single string. Just going to use a very simple test string. And then finish off with a semicolon. And to inspect everything that's in here we can use the two string method of the Swift message class.
So echo, then our message object, and toString. So if I save that, and then load this into a browser, it shows us what will be sent, just right click and View Page Source so we've got everything nicely formatted, and this immediately shows a major difference between using Swift Mailer and the standard PHP mail function. Swift Mailer has automatically created a series of headers, including a unique message ID, the date, which is correctly formatted for an email, then the subject, the from, and the to addresses, and also the content type, and content transferring coding.
That's all done automatically, there's no need to add those headers yourself, it's all done by Swift Mailer. Let's return to the editing program and see the difference between the setTo and addTo methods. Let's just go back, and I'm using setTo on Line Nine. If you want to send to multiple email addresses using setTo, you need to have all the email addresses in the single array. But rather than using setTo, I'm going to change this to addTo, so I'm going to comment out that line, and then on the next line we'll use the arrow operator and chain addTo...
And this takes one or two arguments, the first is the email address, and the second optional argument is the recipient's name, in other words, addTo adds only a single recipient. So I'm going to add that address there... and then the second argument, the name that I want to associate with it, but I can add other addresses. Continue doing addTo. Let's just have another one.
I won't bother with a name for that, and we can add yet another one. And let's just add a name for that. So if I save that, and then go back to the browser, and right click, view the page source, we can see on Lines Five and Six the to header has got all the names added to it, so there is David Powers at firstname.lastname@example.org, email@example.com, and then someone else, firstname.lastname@example.org.
So all the recipients have been added. The important distinction between setTo and addTo is that you can make multiple calls to the addTo method, and each recipient will be added to the to header. On the other hand, if you call setTo more than once, it will override previous calls. To send an email to multiple recipients with setTo you must supply the list of recipients as an array. Set fixes the recipient list, whereas add adds to it.
So let's just go back to the editing program. To summarize, you prepare an email message by creating a new instance of the Swift message class, and you use the set subject, setFrom and setBody methods to add the subject line, the from address, and the message body. To set the recipients, use setTo or addTo. It doesn't matter which order you call these methods, Swift Mailer sorts everything out automatically, and you can use the toString method to inspect the message and its headers.
Swift Mailer is free and feature-rich, and it integrates into any web app written in PHP 5 or 7. Swift Mailer offers a flexible and elegant object-oriented approach to sending emails, including HTML email and bulk email. This PHP tutorial guides developers through the process of installing and loading Swift Mailer, and sending emails using SMTP and encrypted SMTP connections. David Powers teaches secure methods that protect users and support MIME standards. He also introduces advanced techniques for sending attachments, personalizing email, and limiting the send rate. Want to see how Swift Mailer solves a real-world programming challenge? Chapter 6 is a case study that shows how to use Swift Mailer to send an email registration form.
- What is Swift Mailer?
- Installing Swift Mailer
- Preparing basic email
- Sending email via SMTP
- Handing off emails to mail transport agents
- Sending attachments
- Sending HTML email
- Personalizing bulk mail
- Limiting the rate of sending emails