Sending email from PHP
How to send email from PHP so that it arrives and cannot be abused: the mail() function, sending through SMTP with the PHPMailer library, verifying the domain with SPF, DKIM and DMARC, deliverability and a secure contact form.
The mail() function has been in PHP forever and works on most hosting without any further setup. What exactly it does, how to send a message with accented letters, which addresses belong in the From and Reply-To headers and why nothing unverified from a visitor may get into the headers.
Over SMTP, the website logs in to a real mailbox just like a mail program on a computer. The messages then leave from the mail server of your domain and the script learns about an error right away. How to do it with the PHPMailer library.
Anyone can write any sender into an email. Three DNS records – SPF, DKIM and DMARC – let the recipient tell whether the message was really sent by a server authorized for the domain. What they mean, how to set them up and how to check them from PHP.
A message sent without an error does not have to reach the inbox. The most common reasons why emails from a website end up in spam or bounce, what large mail services require and how to send bulk messages so that the domain does not lose its good reputation.
A contact form is an attractive target for robots. They flood you with spam or try to abuse it to send spam to others – and the domain of the website then ends up on blacklists. How to write a form that sends through PHPMailer and withstands all that.