Course
PHP Tutorial for Beginners: Learn in 7 Days
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
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.
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
Following are frequently asked Laravel and PHP related interview questions for freshers as well as...
What is a File? A file is simply a resource for storing information on a computer. Files are...
What is CakePHP? CakePHP is an open-source framework for the rapid development and maintenance of web...
Project Summary This Project will put you in an online corporate setting. You will be coding a demo...
What is a control structure? Code execution can be grouped into categories as shown below...