Sending Email with PHP script is not so hard. There is inbuilt function in PHP for sending emails. Here in this tutorial we will show how to send email in PHP script? Many times we need to send some information to the user in his email account. We can directly send the information to users email.
In this tutorial we will try to send the information of a user entered by him to his email address. With this example you will easily understand how to send emails in PHP Code.
I am going to enter the user’s information via a HTML form and then send it to his email address entered by him. You may don’t find this example useful in real life but you will understand easily how mail function works?
Example – How to Send Email From a PHP Script
Now create a file sendmail.php this file will be used for both creation of form and processing the form. We are not going to use another PHP file to process the information entered by the user.
Example Code for sendmail.php is below:
<title>Send Email in PHP Script | LetITLearn.COM</title>
<h2>Send Email in PHP Script | LetITLearn.COM</h2>
<h3>Enter User's Information</h3>
<form align=”left” name=”info” method=”post”>
<td><input name=”name” size=”25″ type=”text” /></td></tr>
<td><input name=”fname” size=”25″ type=”text” /></td></tr>
<td><input name=”city” size=”25″ type=”text” /></td></tr>
<td><input name=”email” size=”25″ type=”text” /></td></tr>
<tr><td></td><td><input name=”send” type=”submit” value=”Send Mail” /></td></tr>
<!– PHP Code to Process the form and send Email –>
if(isset($_POST[‘send’]))//If form is submitted
//Save the information in variables
//Save the mail information in variables
$subject=”This is a Test Mail from LetITLearn.COM”;
This is test mail from LetITLearn.COM. Find your details :- <br/>
Your Name:- $name <br/>
Your Father’s Name:- $fname <br/>
Your City :- $city <br/>
Your Email:- $mail <br/>
Script by – <a href=http://www.letitlearn.com>LetITLearn.COM</a><br/>
echo $message; //To view what message has been sent.
mail($to, $subject, $message, $headers); // The Mail Function
echo “<br/>Your mail has been sent.”;
<br /><br /><br />
Lets describe the whole process of how to send email in PHP script.
First we have created a form that takes input. When the user click on Send mail button. All information is processed via PHP code. We first save all the fields information into variable. Then we save mail function information into the variable.
$subject variable store the text that will be used as subject of email. $to variable store the email address of recipients. You may also send the mail to multiple address by placing a comma between email IDs. $message variable store your mail body message. You can write simple text email or you can write html emails.
We have echo $message variable to know what message has been sent to user. Then we have user $headers variable. This variable is used to store information about the email. The ‘From:’ section is used to mention the email address from the mail is sent. ‘Reply-to:’ is used to store the reply to email address. When users press reply button, the message will be sent to this mail ID.
‘Content-Type:’ describe the what type of content is being sent. In our case, we are sending a HTML email so ‘Content-Type’ is set to ‘text/html’. Then there is information about email sending client. In our case it is sent from PHP client.
This script works when your PHP server is configured for sending e-mail.
Also read: Make Your Career as An App Developer