Free and Opensource software review, Operating system review, Tips and Tricks, System tweaks, troubleshooting, Tools and Techniques, Hacks

Send Mail Using php

It is a great help for the programmers using php to send mail using a simple function within their script. php mail() function is the simple way to send an email directly from the script. This function returns true value if the mail have been accepted to send by the server to the recipient. It is a very simple php function and can be used in variety of fields where is required to send automated reply to an action by the user. The simplest way of using this mail() function is

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

to – this field is a required field and it should contain email address or addresses of the recipients  of the mail.

subject – this field is a required field and contains the subject of the mail to be sent.

message – this is also a required field and contain the message to be send.

headers – This is an optional field and includes additional information like  From, Cc, and Bcc.

parameters – this is also an optional field which specifies additional information to the function.

Simple PHP mail Program

<?php
$message = “This is a message.The line separator should be used to separate two line. n This is the second paragraph of the message. “;
mail(“somebody@something.com”,”Subject of mail”,$message);
?>

The above program sends simple text message specified as string $message to somebody@something.com.

mail program to send custom message to custom email

Sending custom message to custom email means, there should be a field to enter email address and the message to be sent. The user input can be obtained using input form just like the one shown below.

<form method=’post’ action=’mail.php’>
Email: <input name=’email’ type=’text’ /><br />   <!– field to enter recipiant email –>
Subject: <input name=’subject’ type=’text’ /><br /> <!– field to enter subject of the mail –>
Message:<br />
<textarea name=’message’>  <!– text are to enter the message –>
</textarea><br />
<input type=’submit’ />
</form>

The php code for sending the mail should be copied into a text file named mail.php

<?php
$email = $_REQUEST[’email’] ;
$subject = $_REQUEST[‘subject’] ;
$message = $_REQUEST[‘message’] ;
if ( mail(“someone@example.com”, $subject,$message, “From:” . $email) )  /* mail function returns true if the server have accepted the mail for deliver ( not necessary to send) */
echo “your mail have been successfully send”;
else
echo “Failed to send message”;
?>

The above code consist of two part the first html portion is used to get user input and that information is passed to the php file to send the mail.

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *