E-mail in PHP

E-mail in PHP


Posted in : PHP Posted on : January 10, 2011 at 5:40 PM Comments : [ 0 ]

This section contains the detail about E-mail in PHP.

E-mail in PHP

For sending e-mail to any person, PHP provide a function known as mail().

The syntax of the mail() function is given below :

mail(to, subject, message, headers, parameters)

Here,

 to - Compulsory field, It should contain email of the receiver / receivers.

subject -  Compulsory Field, This field should contain the subject of the email. This parameter cannot contain any newline characters

message -  Compulsory Field, This field contain message body. Each line should be separated with a LF (\n). Lines should not exceed 70 characters.

headers -   Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).

parameters - Optional. Specifies an additional parameter to the sendmail program.

Example :

This example contains a html contact form and a PHP script to send mail. This will give you a clear idea how to use it.

Html form

<HTML>
<HEAD>
<TITLE>Send Email</TITLE>
</HEAD>
<BODY>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td><strong>Send Email</strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_mail.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Message Body</td>
<td>:</td>
<td><textarea name="message" cols="50" rows="4" id="message"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="e_mail" type="text" id="e_mail" size="50"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</BODY>
</HTML>

send_mail.php

<?php
session_start();
$from ="admin@devmanuals.com";
$to = $_POST['e_mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$header="from: Admin <$from>";

//sending email
$send_mail=mail($to,$subject,$message,$header);

// display message if message sent to your email
if($send_mail){
echo "Message successfully sent!";
}
else {
echo "Message delivery failed...";
}
?>

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics