PHP
12 BEST PHP Books (2021 Update)
PHP is a server-side scripting language used to develop static and dynamic websites or web...
PHP mail is the built in PHP function that is used to send emails from PHP scripts.
The mail function accepts the following parameters;
In this tutorial, you will learn-
The PHP mail function has the following basic syntax
<?php mail($to_email_address,$subject,$message,[$headers],[$parameters]); ?>
HERE,
PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail.
On a hosted server, the SMTP settings would have already been set.
The SMTP mail settings can be configured from “php.ini” file in the PHP installation folder.
Configuring SMTP settings on your localhost Assuming you are using xampp on windows, locate the “php.ini” in the directory “C:\xampp\php”.
Php Mail Example
Let’s now look at an example that sends a simple mail.
<?php $to_email = 'name @ company . com'; $subject = 'Testing PHP Mail'; $message = 'This mail is sent using the PHP mail function'; $headers = 'From: noreply @ company . com'; mail($to_email,$subject,$message,$headers); ?>
Output:
Note: the above example only takes the 4 mandatory parameters.
You should replace the above fictitious email address with a real email address.
The above example uses hard coded values in the source code for the email address and other details for simplicity.
Let’s assume you have to create a contact us form for users fill in the details and then submit.
Let’s create a custom function that validates and sanitizes the email address using the filter_var built in function.
Filter_var function The filter_var function is used to sanitize and validate the user input data.
It has the following basic syntax.
<?php filter_var($field, SANITIZATION TYPE); ?>
HERE,
The code below implements uses a custom function to send secure mail.
<?php
function sanitize_my_email($field) {
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email = 'name @ company . com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail ';
$headers = 'From: noreply @ company. com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
echo "Invalid input";
} else { //send email
mail($to_email, $subject, $message, $headers);
echo "This email is sent using PHP Mail";
}
?>Output:
Emails can be intercepted during transmission by unintended recipients.
This can exposure the contents of the email to unintended recipients.
Secure mail solves this problem by transmitting emails via Hypertext Transfer Protocol Secure (HTTPS).
HTTPS encrypts messages before sending them.
PHP is a server-side scripting language used to develop static and dynamic websites or web...
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
Project Summary This Project will put you in an online corporate setting. You will be coding a demo...
What is Regular expression in PHP? PHP Regular Expression also known as regex are powerful pattern...
What is CakePHP? CakePHP is an open-source framework for the rapid development and maintenance of web...
What is a Function? A function is a reusable piece or block of code that performs a specific...