PHP send email using Inbuilt PHP MAIL() function. This tutorial shows you how you can send email in PHP using inbuilt PHP mail() function.
If you work with PHP, you need to send emails to users like when the user registered with us, send reset password link in the mail, if any offers send to users by email, send invoice or pdf, welcome mails, etc.
How to Send Email from Localhost in PHP
Using the PHP inbuilt mail() function, You can send text or HTML in emails from localhost in PHP.
Sending email is a very common process for any web application. Such as welcome email, reset password link, invoice or PDF if there is an offer, etc.
Here you will learn how to send an mail from your web application to one or more users using the PHP built-in mail () function. either in plain-text form or in HTML format.
The basic syntax of PHP mail function is:
mail(to, subject, message, headers, parameters)
Now we demonstrate to you the mail function parameters:
|Parameter||Required or Optional||Description|
|Yes||The recipient’s email address.|
|Yes||Subject of the email to be sent. This parameter i.e. the subject line cannot contain any newline character (|
|Yes||Defines the message to be sent. Each line should be separated with a line feed-LF (|
|No||This is typically used to add extra headers such as “From”, “Cc”, “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (|
|No||Used to pass additional parameters.|
Send Plain Text In Emails – PHP
The easy way to send an email with PHP is to send a text in the email. In the below-given example, we first declare the variables — recipient’s email address, subject line, and message body. After that, we pass these define variables to the mail() function to send the email.
Example of sending plain text in emails
The below example sends plain text in emails using the PHP Mail() function.
Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.
Once less secure apps are enabled; now you can use your Gmail for sending the emails.
Send HTML in Emails Using PHP Mail Function
When you send a simple text message in the mail using PHP mail function, that is treated as simple text. We are sending a well-designed mail to users, so that time we will send HTML in mail
If you want to send an HTML in emails using PHP mail function. So don’t worry, the email sending process will be the same. However, this time we add additional headers as well as an HTML formatted message.
Example of sending HTML in the email using PHP mail()
'; $message .= '
Hi There!'; $message .= '
This is a testing mail with html?'; $message .= '