How to create Mail Function using PHP?

Asked By 220 points N/A Posted on -
qa-featured

Hi everybody,

I wish all are well by the grace of god. I am a student (new Student) of web developer in a Web Design Training Center. I have already complete HTML, Flash, Adobe Photoshop. Recently started PHP. It has appeared very hard to me.

Now I want to create a mail function by PHP. Actually I create few web site by free domain and free hosting.

Now I want to create a feedback tab.

For this reason PHP mail function is the most important for me.

Anybody help me to create a mail function by PHP. 

Thanks Advance for all visitor.

SHARE
Best Answer by Amarion
Answered By 0 points N/A #107719

How to create Mail Function using PHP?

qa-featured

PHP has a built in function for sending emails, you can simply call that function for sending mails from your website. The PHP ‘mail()’ function will return a Boolean result depending on whether the mail has been successfully sent or not. If the message was successfully sent then it will return a Boolean ‘true’ otherwise it will return a Boolean ‘false’.

The syntax for the PHP mail function is as follows:
 
<?PHP
 
mail (‘[email protected]’, ‘ subject line’, “body of the mail”);
 
?>
 
These are the minimum parameters required in the PHP mail function for sending an email. The first parameter is the address of the recipient to which the mail has to be sent. The second parameter is the subject of your email, this will be displayed as the subject of your email in the receivers email client.
 
The third part is the body of your mail. The lines in the body of your mail should be separated by the new line character ‘rn’. The PHP mail function will automatically add the senders address by taking the details from the SMTP server of your host. One example for you is given below.
 
<? PHP
 
 
$subject= ‘Sample mail’;
 
$body= “Hello user rn This is a sample email”;
 
$result=mail($to, $subject, $body);
 
if($result)
 
{
 
echo “mail successfully sent”;
 
}
 
else{
 
echo “mail sending failed”;
 
}
 
?>
 
The above code will send a sample email to [email protected] and if the message was successfully send it will display the message” mail successfully send” otherwise it will display the message “mail sending failed”.
 
You can give multiple addresses by entering a comma in between them, for example
 
 
If you want to format the appearance of your email, then you can add additional headers in the mail function for formatting the mail. For example
 
<?PHP
 
 
$subject= ‘Sample mail’;
 
$body= “Hello user rn This is a sample email”;
 
$header= “From: [email protected] tr n bcc: [email protected] rn content-type: text/plain”;
 
$result=mail($to, $subject, $body, $header);
 
if($result)
 
{
 
echo “mail successfully sent”;
 
}
 
else{
 
echo “mail sending failed”;
 
}
 
?>
 
This will send a blind carbon copy of your mail to [email protected] and it defines the content of the mail to be plain text. Similarly we can send html content and other content types in the mail using the PHP mail function.
 
The header field can contain the following fields, senders name, senders email address, a reply address or a bounce address, X-mailer and its version number, MIME version, Content-type, Char-set, Content-transfer-encoding, copy and blind-copy recipients. Similarly you can attach a file in you email message by adding the appropriate headers in your mail functions.
 
You can use a form to get the contents of the mail and then save it into a variable and then you can use it in the PHP mail function. You can store the email addresses in a database and then you can use the PHP mail function to send the mail to the addresses taken from the database, so it can be used as a small mailing list manager.
Best Answer
Best Answer
Answered By 0 points N/A #107720

How to create Mail Function using PHP?

qa-featured

Source code is attached;

Live working demo is available at:
 
Free web hosting, no ads or banners   that will send feedback at [email protected]
 
You can enter the required email in the highlighted area of the coding.

Login/Register to Answer

Related Questions