Technologies advance – so does the world every day. But some things stay forever. Like sending emails with PHP. You might be skeptical and thinking even in 2022? Yes, even in 2022. W3Techs reports that, as of January 2022, “PHP is used by 78.1% of all the websites whose server-side programming language we know.” Show
That said, let’s check out this tutorial that will help you send emails from PHP today. Send mail from PHP: three important things that you need to know right awayOne, there is a built-in PHP mail() function and it is quite simple. Two, although PHP’s built-in mail function () is very simple, it provides limited functionality for sending emails.You won’t be able to add attachments to your email, and building a beautiful HTML template with embedded images will be a tricky task as well. Three, the PHP mail function () sends emails from your website which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. PHP mail() also does not usually allow you to send mail using an SMTP server externally, and it does not support SMTP authentication. What options do you have to send emails in PHP?There are four popular ones.
We’ll go step-by-step through all of them. PHP built-in mail function ()Mail() is a wrapper on top of the sendmail utility, so sendmail has to be installed in the system first. Here’s what you can do with PHP built-in mail function():
Keep in mind that when you send emails with mail(), you may come across some grave deliverability issues. The messages dispatched will not benefit from the SPF and DKIM setup on your domain, therefore, the messages will likely be treated as spam by the receiving MTA (Mail Transfer Agent). Thus, overall deliverability of email messages sent via PHP mail () is not guaranteed. Moreover, you won’t receive bounceback messages if there is a delivery failure. If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic PHP script syntax and its main parameters. Syntax and parametersThe PHP mail syntax is pretty simple:
It uses the following parameters:
The additional headers indicate other recipients or copies of your message like CC or BCC. They can be an array where key is a header name and value is a header value. Or they can be a string. In this case headers should be separated with a CRLF (\r\n).
For more details and additional parameters, refer to the PHP documentation. Sending HTML email using PHP mail() functionThe body of the message can be written in HTML. However, as we’ve mentioned above, it should be simple. In the PHP mail function(), the HTML message part will look like this:
It’s important to remember that to send HTML mail, you need to set the Content-type header: Simple Transmission Protocol (SMTP)Where do I specify the SMTP settings? This is a fair question. Go to the PHP file installation folder and configure them in the php.ini file. But this will only work for localhost or XAMPP like solutions because as we have already mentioned, PHP mail() function does not support SMTP authentication and doesn’t allow sending messages via external servers. To send mail without SMTP, you could also refer to a third party-service like Mailtrap, SendGrid, or Mailgun. Sending multiple emailsTo send your message to multiple recipients, specify their email addresses in “$to” = parameter separating them with comma(-s). It’s the only suitable method with a native mail() function. If you need to send a large volume of messages in a loop, try an external mailing package like Symfony Mailer. Or the third-party solution. How to send emails using PHP via the contact form?There are also a couple of ways to do this. And this topic needs a special tutorial. Either you run a WordPress website, or need a simple contact form, check out this encoding for a PHP contact form with Google reCaptcha. We explain how it works in detail, plus we go through many other ways to create contact forms in PHP in our special guide on PHP code to send email from a contact form. Tune in.
How to send emails with PHPMailer?PHPMailer is the classic and the most popular email sending library for PHP. It deserves a separate article and a tutorial. To get a detailed overview of the php code in PHPMailer read our guide on how to send emails using PHPMailer. What you can do with PHPMailer
PHPMailer is protected against header injection attacks and automatically validates emails. Now let’s send, say, a hotel booking confirmation with PHPMailer:
Pay attention: for this email message we use the following piece of code
to set the mail server, SMTP port, and enable email authentication. PHP mailing packagesAs we have already mentioned, the native PHP mail() function is not designed for creating email templates and sending a large volume of emails. Moreover, it causes some serious deliverability issues. So we would recommend using some external mailing packages instead. For 2022, Pear:: Mail and Swift Mailer are not a thing anymore (as they are outdated now), so the obvious choice would be Symfony Mailer. Symfony Mailer & Mime components establish a solid system for creating and sending emails – complete with support for multipart messages, Twig integration, CSS inlining, file attachments etc. We have created a step-by-step guide for Sending emails in Symphony: from installation to transport set up, and creating and sending messages. Feel free to use it to your davantage. Do not hesitate to turn to current 6.1 version documentation for specific details. Mind that here we show a standalone usage of this package, and it’s already integrated into Symfony and into Laravel framework. So using it is simpler while working with the frameworks altogether.
Sending emails in PHP with third-party email servicesIn case your application requires automated email sending and email analytics, it is always better to go for the third-party email services. Why? It is faster, it is less labor and resource-consuming, and let’s be honest: it gives you more control over email infrastructure. There are many third-party email services you can integrate your app with, such as Sendgrid, Mailgun etc. But, the Mailtrap Delivery Platform might arguably be the one you actually need. Using Mailtrap to send emails with PHPIf you’re looking for a reliable and hassle-free email sending service with PHP, you should probably consider the Mailtrap Email API. It’s an Email API/SMTP Relay that allows for up to 60 days of email logs for improved troubleshooting (saving your hard data during this time span). Dashboards with a clear snapshot of the state of your email infrastructure are always at hand for analytics. Another advantage of Mailtrap email API is our timely email deliverability alerts that give you greater control over your email infrastructure and domain authority. This means that if anything goes wrong with your deliverability, you get an alert with a color-coded table plus insights on where to inspect a deliverability issue. Yet, Mailtrap sends you regular weekly reports on your deliverability performance to keep you up to date with your email sending and deliverability performance. Mailtrap API integration for PHPGo to the Sending Domains tab, choose API/SMTP Integration, go for the API one, and select PHP from the library.
Now you can start sending. Try Sending Emails in PHP with Mailtrap for Free Server configurationHere is how your SMTP server configuration to start sending with PHP would look like in Mailtrap for PHPMailer (go to SMTP credentials at Mailtrap interface, then just copy and paste them):
Three common issues with PHP mailsWe’ll cover only the basic issues here. In case you have a specific problem not mentioned here, refer to the PHP documentation, or the sending service providers’ support. Wrongly displayed charactersLet’s say the email doesn’t render German characters (umlauts) properly. Then, you need to set the Content-Type and the Charset in the headers of the email:
Mostly, UTF-8 is your best choice. Then, add the following header too:
Now, you can use both UTF-8 and Base64 to properly encode the subject line or the recipient name if they require specific characters.
And don’t forget to Base64 encode the email message too:
Windows and Linux configurationsTroubles might come if your PHP is not configured properly in the php.ini file. For Windows users, it should somewhat look like this:
Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive. The configuration for Linux should look something like this:
PHP mail function is not sending the emailThis could be pretty tricky. However, here are a few things you can do to handle it:
Anyway, troubleshooting and testing needs some special attention and in-depth analysis. Here, you can find out more about how to test emails sent from PHP. Final considerationsIn this article, we have described the basic PHP email sending principles, syntax, and parameters. We’ve also reviewed the main ways of sending emails with PHP: its built-in mail function and the most popular external mail package. However, in many situations, you might consider using a third-party mail service integration like Mailtrap to save time, money, and effort. How do I set the name of an email sender via PHP?$to = "[email protected]"; $message = "Your message here."; $subject = "Your subject"; mail($to,$subject,$message,$header, '-f [email protected] -F "Jack Sparrow"') or die(); Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string.
How send mail in PHP explain in detail?PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );
What is PHP mail () function?PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.
How configure SMTP in PHP?Writing the PHP Code to Send Email using Gmail SMTP. Step 1: Download PHPMailer library from this github link. ... . Step 2: Writing the PHP Code to make an SMTP connection. ... . Step 3: Include packages and files for PHPMailer and SMTP protocol: ... . Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:. |