How To Send Emails In PHP
Core function: mail()
Sending email messages are very common for a web application, for example, sending welcome email when a user create an account on your website, sending newsletters to your registered users, or getting user feedback or comment through website’s contact form, and so on.
PHP has a built-in mail function that can be used for creating and sending email messages to one or more recipients dynamically. Plain-text form or formatted HTML is available but does have some challenges based on the mail server recieving the email .
mail(to, subject, message, headers, parameters)
REQUIRED PARAMETERS
to
The recipient’s email address.
subject
The recipient’s email address.
message
Defines the message to be sent. Each line should be separated with a line feed-LF (\n
). Lines should not exceed 70 characters.
OPTIONAL PARAMETERS
headers (optional)
This is typically used to add extra headers such as “From”, “Cc”, “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n
).
parameters (optional)
Used to pass additional parameters.
Sending Plain Text Emails
The simplest way to send an email with PHP is to send a text email. In the example below we first declare the variables — recipient’s email address, subject line and message body — then we pass these variables to the mail()
function to send the email.
EXAMPLE CODE
<?php
$to = ‘office@email.com’;
$subject = ‘TEST EMAIL’;
$message = ‘Hi, Intereseted in meetings?’;
$from = ‘youremail@email.com’;
// Sending email
if(mail($to, $subject, $message))
echo ‘Your mail has been sent successfully.’;
else
echo ‘Unable to send email. Please try again.’;
?>
Sending HTML Formatted Emails
When you send a text message using PHP, all the content will be treated as simple text. We’re going to improve that output, and make the email into a HTML-formatted email.
To send an HTML email, the process will be the same. However, this time we need to provide additional headers as well as an HTML formatted message.
<?php
$to = ‘office@email.com’;
$subject = ‘TEST EMAIL’;
$from = ‘youremail@email.com’;
// To send HTML mail, the Content-type header must be set
$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;
// Create email headers
$headers .= ‘From: ‘.$from.“\r\n”. ‘Reply-To: ‘.$from.“\r\n” . ‘X-Mailer: PHP/’ . phpversion();
// Compose a simple HTML email message
$message = ‘<html><body>’;
$message .= ‘<h1 style=”color:#f40;”>Hi Jane!</h1>’;
$message .= ‘<p style=”color:#080;font-size:18px;”>Can we meet?</p>’;
$message .= ‘</body></html>’;
$message = wordwrap($message, 70, “\r\n”);
// Sending email
if(mail($to, $subject, $message, $headers)
echo ‘Your mail has been sent successfully.’;
else
echo ‘Unable to send email. Please try again.’;
?>
COMMON PROBLEM
"message has lines too long for transport" issue
A frequent issue for php generated HTML emails is a bounced email message from the associated receiving server with the notice: ‘message has lines too long for transport‘.
The cause of this problem is that emails are really a blending of two worlds. HTML and the internal command structure of the email base. Headers and plain email use the /r/n command structure to indicate a new line. Where as HTML uses Table breaks or simple <br> formatting. But to the email server, these HTML commands are unrecognized. The server has no idea that the line is wrapping and thus any HTML email, that has a total character length of more than 70 characters, will generated this error.
The fix is simpler than you would imagine: Add the following line to your code:
$message = wordwrap($message, 70, “\r\n”);