In this article, you will find the answer to the question “How to use PHP mail() function and how to send emails using PHP?”
The mail() Function in PHP
Email is an important part of communication and frequently used by web applications to notify users about important events and actions.
When we register with some application we get an instant notification as Welcome Email.
Almost every website has a contact us page and there is a form with some fields.
Users use this form to communicate with customer care or for any inquiry or to get any Quotations.
We can use the PHP built-in mail() function for sending email messages to many recipients using PHP application.
We can send Plain text messages or HTML rich text messages with styled and multicolor design. In PHP mail() function is used as following:
mail(to, subject, message, headers, parameters);
The PHP mail() function has the following parameters:
to: This email address of the recipient.
subject: Subject of the email to be sent.
message: This is the message body and main content of the email.
headers: This is used to add headers such as “From”, “Cc”, “Bcc”.
parameters: This is used to add additional parameters.
Simple Mail Transmission Protocol (SMTP) Configuration:
PHP mailer uses the Simple Mail Transmission Protocol (SMTP) to send mail.
The SMTP settings would have already been configured.
The SMTP mail settings can be configured from “php.ini” file in the PHP installation folder.
Sending Plain Text Emails using mail() Function with Example:
We can send simple text messages using PHP mail() function. An example is given below:
Sending HTML Emails using mail() Function with Example:
We can send rich text styled HTML messages using PHP mail() function. An example is given below: