- Sending emails with PHP using mail() function
- Syntax
- Breaking it Down
- to
- Using heredoc instead of quotes
- headers
- Parameters
- Examples
- Example 1: Sending Plain-text email
- Example 2: Sending HTML email
- Example 3: Sending an email with variables in the message body and heredoc message string formating.
- Example 4: Sending email from contact form
- Note
- Related Articles
- PHP mail() Function
- Syntax
- Parameter Values
- Technical Details
- More Examples
Sending emails with PHP using mail() function
PHP mail() function is a built-in function in PHP that allows sending email using a local sendmail program.
Whenever you call the mail() function, it invokes a local sendmail program. If your website is hosted on an online server, the sendmail program is usually configured by default by the system administrator.
The sendmail package is also in-built in local servers (eg. XAMPP, WAMPP, easyPHP etc). However, it is not enabled by default and it will need to be configured in order to work.
The mail() function allows you to send emails directly from a script.
Syntax
This is what you need to start sending emails via the mail() function:
Breaking it Down
to
This is a required parameter specifying the email recipient. It should be a valid email address to which the email is sent eg. john@webdevsplanet.com.
Ways in which you can set the to parameter:
Subject
This is a single line of text that gives a summary of what the email is about. It is a required parameter and cannot contain any newline characters.
Message
This is a required parameter that defines the actual message of the email. The message can be either plain-text or HTML format.
i). Plain-text message
For plain-text message, each line should be separated with a LF ( ) and should not exceed 70 characters length.
In case the message has longer lines than 70 characters, use the wordwrap() function.
The wordwrap() function wraps a string into new lines when it reaches a specific length (ie, it adds “ ” characters).
Having our message held in a variable named $msg, we use the wordwrap() function below to break our message into new lines after every 70 characters length.
If a full stop is found at the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot:
ii). HTML message
When sending a HTML formatted message you need to set the Content-Type to “text/html” using a header. This will tell the recipient mail client that the received message content is in HTML format and it should be rendered as so. If you omit this header the message will be rendered as plain-text with HTML tags.
Happy Christmas holidays Hello James, I wish you and your entire family happy Christmas holidays.
"; ?>
Single quotes(') works pretty much the same as the double quotes(") in the above examples to enclose the message ($msg) string.
However, you are likely to use quotes within the body of your message. In this case you will need to escape them to avoid errors. Use double quotes to enclose your message in case your message contains single quotes in it and vise versa.
Using variables within the message
You can add variables within your message in a number of ways.
is a student from "; //Method 4 $message = $name, " is a ", $course, " student from ", $country; ?>
Using heredoc instead of quotes
I said above that when your message string contains a single quote in it you should use double quotes to enclose in and vice versa.
But what if the message string contains both single and double quotes in it?
HEREDOC comes to your rescue.
The heredoc is a string method for multi-line strings and an alternative to using quotes. It is preferable because it allows the usage of both single and double quotes within your string.
The closing identifier must begin in the first column of the line. The line with the closing identifier must contain no other characters, except a semicolon (;), ie, make sure that there is no space before EOD; or after it.
headers
Headers are an an optional parameter in mail() function used to pass extra and vital information such as:
- Sender address
- Reply-to address
- Carbon copy (Cc)
- Blind carbon copy(Bcc)
- Content-type
- MIME-Version
- PHP version
- etc
When using multiple headers, always separate them with a CRLF ( ).
If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.
Example of setting headers:
" . "\r\n"; $headers .= "Reply-To: email@example.com" . "\r\n"; $headers .= "Cc: recipient2@example.com" . "\r\n"; $headers .= "Bcc: recipient3@example.com" . "\r\n";
Parameters
This is an optional parameter that specifies an additional parameter to the sendmail program. You will rarely need to use so we won't use it in our examples.
Examples
We have multiple working examples which you can copy and use in your projects to do tests.
You need to upload the code to an online hosted server or configure the sendmail appropriately in your local server (XAMPP, WAMPP, EasyPHP etc) for it to work.
Note:
Remember to replace the dummy data in the examples with your valid details for them to work.
All successful sending of emails with print out “Message sent successfully” while the failed ones will print out “Message was not sent”.
In case you don't find the emails in the inbox after sending, kindly check also in the spam/junk folder.
Example 1: Sending Plain-text email
Example 2: Sending HTML email
This email contains HTML Tags!
First Name John Last Name Doe Email email@example.com