Monday, January 25, 2021
Home PHP Sending Emails in PHP

Sending Emails in PHP

Sending Emails In PHP
Sending Emails In PHP

Sending emails in PHP is very easy. You can use PHP mail function for this purpose. Lets write a simple program which will send an email.


$to = ''; //Email address to which email will be sent.

$subject = 'Sending emails in PHP'; // Subject of the email

//Email body or message
$message = 'Hello, This email is sent to you using PHP mail function.
           Have you received this email? ';

//Send email
if( mail( $to, $subject, $message) )
  echo 'Mail is sent.';
  echo 'An error occurred while sending mail.';




Now lets analyze the above code line by line.

For sending emails in PHP, we use PHP mail function. This function takes a number of arguments and returns true if the mail is sent or returns false, if the mail is not sent. To send an email using PHP mail function, three arguments should be supplied to the PHP mail function, which are email address to which mail will be sent, a subject for the mail and a message for the mail.

At line 3, we have declared a variable $to and assigned our desired email address to which mail will be sent. Similarly subject for the mail is assigned to $subject at line 5 and message is assigned to $message at line 8.

Now the main action is performed at line 12 .

 if ( mail ( $to, $subject, $message ) )

We have supplied $to, $subject and $message to the mail function. Here we have place the mail function in if-else statement to check whether the mail is sent or not. If mail is sent then the message “Mail is sent.” is displayed, and if mail is not sent, then the message “An error occurred while sending the mail.” is displayed.

So you can see above that how easy sending emails in PHP is ? ūüėČ

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....