Why Would I Want to Use Email with PHP?
PHP is a useful website building tool. It gives the website the ability to automatically run scripts when a button is pushed or someone interacts with a particular part of a site.
Using PHP to send emails is often used for forms embedded on the site. When the form is filled and submitted, the PHP script will run, grab the information from the form, and send out an email.
Depending on how you configure your script, that email can be anything from a forwarded message to you to a greeting to the person who submitted the information.
Where Do I Put PHP Script?
The PHP script will go into your site’s HTML, usually within the <body> element.
How Do I Send Email with PHP?
Check out PHP.net’s mail function documentation.
If you would like some examples, see W3schools PHP mail function explanation.
Parameters
Parameter |
Use |
Variable Type |
Required? |
$to | Specifies where the email will be sent. | String | Yes |
$subject | Specifies the email subject.
This parameter will display in the email’s subject line. |
String | Yes |
$message | Specifies the body of the email.
Each line should be separated by a \r\n . If you exceed 70 characters, use wordwrap. |
String | Yes |
$additional_headers | Specifies any extra headers, like From, CC, and BCC, that are inserted at the end of the email header | String | Optional |
$additional_parameters | Specifies any additional parameters | String | Optional |
wordwrap | Wraps strings that are longer than a certain amount of characters.
Use in conjunction with $message. See an example here. |
String | Optional |
CRLF
CRLF stands for “Carriage Return Feed Line,” which signals the end of a line. CLRF is written as \r\n.
Common Use Examples
To send a message to a specific email address:
<?php $message = “New customer.\r\nGo to thisSite.com to see new customer info.\r\nThanks,\r\nSite Team”; mail(‘[email protected]’, ‘Email Subject’ , $message); ?>
If the message is longer than 70 characters:
<?php $message = “This is a message longer than seventy characters. Use wordwrap below to make things work.”; $message = wordwrap($message, 70, “\r\n”); mail(‘[email protected]’, ‘Email Subject’ , $message); ?>
If the email has multiple headers:
<?php $to = ‘[email protected]’; $subject = ‘Email Subject’; $message = ‘Your message will go here.\r\nDon’t forget the CRFL for lines\r\n’; $headers = ‘From: [email protected]’ . “\r\n” . ‘Reply-To: [email protected]’ . “\r\n” . mail($to, $subject, $message, $headers); ?>
Using the Filter Function for Forms
Using the Filter Function for Forms
If you are using this in a form, you should use the filter function. Filters are used to “sanitize” or validate data that comes through your form.
Email forms are a common target for abuse. It’s no problem if the user enters information as they are supposed to, but sometimes they can do things that break the script.
Filters help with that. For example, if a user inputs an invalid character into an email, you can use the filter function with an if-else statement to make sure the email is valid. Check out W3School’s PHP filter explanation to learn more about filters.
Example:
If the input from a form was:
email@@example.com
You would use this code to filter any invalid characters in the address. In this case, the second "@" is invalid.
<?php // form populates variable with user email $email = "email@@example.com"; // Checks email for invalid characters if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo("$email is a valid email address."); } else { echo("$email is not a valid email address."); } ?>
The output would be:
email@@example.com is not a valid email address.
filter_var() initiates the filter function, and then you can use a filter constant to tell filter_var() what you want to filter.
Check out PHP’s list of filter constants. This list gives you an idea of filter_var() uses. Correct usage of filter_var() can protect your form from user error and keep your PHP scripts running smoothly.