Join us
@sofiatarhonska ă» Sep 16,2022 ă» 3 min read ă» 1173 views ă» Originally posted on mailtrap.io
We see numerous requests for guides on sending emails with some popular frameworks and libraries using an SMTP server, so we are launching a series of âhow toâ posts aimed at helping you painlessly configure the mail function in your application.
Today we are starting with PHP, one of the most popular web development languages.
There are two basic ways of sending emails with PHP: a built-in mail function and external mail packages.
PHPâs built-in mail function () is very simple, but 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.
The other side of the PHP mail function () is that the email is sent from your web server, which may cause issues with deliverability due to security concerns such as suspicion of spam and blacklisting. The best way to overcome this problem is sending messages via an SMTP server, however, this functionality is limited as well. PHP mail() does not usually allow you to use the external SMTP server and it does not support SMTP authentication.
Hereâs what you can do with PHPâs built-in mail function():
It is suitable for simple, mostly text-based notifications in your local environment. If you need to communicate with your appâs users, it is better to install an external mailer package.
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 code and its main parameters.
The PHP mail syntax is pretty simple:
<?php
mail($to_email_address,$subject,$message,[$headers],[$parameters]);
?>
It uses the following parameters:
Note that headers are optional, except for the âfromâ header: it must be specified, otherwise, you will receive an error message like Warning: mail(): âsendmail_fromâ not set in php.ini or custom âFrom:â header missing.
You can use additional headers to change the mail âFromâ address and set the âReply toâ address.
For more details and additional parameters, refer to the PHP documentation.
The 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 part will look like this:
$message = '
<html>
<head>
<title>Review Request Reminder</title>
</head>
<body>
<p>Here are the cases requiring your review in December:</p>
<table>
<tr>
<th>Case title</th><th>Category</th><th>Status</th><th>Due date</th>
</tr>
<tr>
<td>Case 1</td><td>Development</td><td>pending</td><td>Dec-20</td>
</tr>
<tr>
<td>Case 1</td><td>DevOps</td><td>pending</td><td>Dec-21</td>
</tr>
</table>
</body>
</html>
';
Itâs important to remember that to send HTML mail, you need to set the Content-type header:
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
Where do I specify the SMTP settings? This is a fair question. Go to the PHP installation folder and configure them in the âphp.iniâ file. But this will only work for localhost or Xmapp 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.
There are some other, rather haphazard options but we wonât promote them here. Alternatively, we recommend using external PHP mail packages for sending emails via an external SMTP server.
Better yet, you can use Mailtrap Email Delivery and weâll provide all the necessary SMTP settings for you. You just need to copy-paste the SMTP settings, verify your domain, and start sending.
To learn about sending multiple emails, PHP mailing packages and PHP Mailer head to our tutorial about how to send mail in PHP on Mailtrapâs Blog.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.