How to send a simple email using the PHP mail() function
An email is an electronic message sent via the internet. There are different platforms today with which we can send and receive emails.
Apart from composing your emails on these various platforms, you can compose emails in your code and send them to these platforms. This can be done using the built-in PHP mail() function.
A common use case
Almost all modern applications today ask for your email address to keep in touch with you. Most of the platforms’ messages are broadcast messages and have already been prepared. They can only be sent when some given conditions are met. You can do this with a simple PHP function called the mail() function.
PHP mail() function
mail() is an inbuilt function in PHP with which we can send emails, be it an HTML email or a plain text email.
Syntax
The following is the function prototype:
mail(
string $to,
string $subject,
string $message,
array|string $extra_headers,
string $_params,
)
The function can take five parameters:
Mandatory parameters
There are three mandatory parameters of the mail() function and they include:
to
This parameter specifies the email address of the receiver. This must be a valid email address in formats such as: receiveraddress@samplemail.com
subject
This will be the title of your message.
message
This will contain the content of your mail.
Optional parameters
The optional parameters are only two and it is a matter of choice to include them.
headers
These are extra email header items which can be a set of strings or an array that you can insert at end of your email header. Examples include From, Cc, and Bcc which are typical of emails.
Additional parameters
You can indicate additional parameters that you would like to add to your email function such as additional command line flag options. This depends on your configuration in the php.ini file in the [sendmail] section.
You have to configure your
php.inifile appropriately to successfully send emails. You can see how to do that in this shot if you use XAMPP.
Code
The following code snippets show how to send emails as plain text or as HTML:
<?php//declare variables for use as function params.$to = 'edpresso@email.com';$subject = 'Shot submitted notification';$message = 'I have successfully written my first shot';// Sending email$emailsent = mail($to, $subject, $message)if($emailsent){echo 'Email successfully delivered.';} else{echo 'Unable to send email. Please try again.';}?>
<?php$to = 'edpresso@email.com';$subject = 'Shot submitted notification';$from = 'chidera@email.com';// Set Content-type header to be able to send html email$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;">Good day Edpresso team</h1>';$message .= '<p style="color:#080;font-size:18px;">I just successfully submitted my first shot</p>';$message .= '</body></html>';// Sending emailif(mail($to, $subject, $message, $headers)){echo 'Your mail has been sent successfully.';} else{echo 'Unable to send email. Please try again.';}?>
Things to note
- Each line in any of the arguments must not exceed 70 characters.
- If it exceeds 70 characters, you can use the
"\r\n"formatting symbol to write multiple lines. - To send a complex HTML email and a large loop of email at once, it is more efficient to use other inbuilt provisions like the
PEAR::MailorPEAR::Mail_Queuepackages or third party provisions likesendgrid