SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

PHP Mail


By using PHP Mail can be posted from a web page. This is a very common requirement. We can use PHP mail function to send mail to different address. This function is required for sending newsletters, designing feedback forms, signup welcome messages and in many more uses. PHP mail function can be managed from php.ini file.
Here it is how it works.
mail("rasmus@lerdorf.on.ca", "My Subject", "Line 1\nLine 2\nLine 3");
If a fourth string argument is passed, this string is inserted at the end of the header. This is typically used to add extra \n will add extra header with a new line.
If the fifth parameter is supplied, PHP will add this data to the call to the mailer. This is useful when setting the correct Return-Path header when sending mail. extra header mail("nobody@aol.com", "the subject", $message, "From: webmaster@sitename\nReply-To: webmaster@sitename\nXMailer: PHP/" . phpversion());

With the fifth parameter you can set additional command line parameters to the actual mailer. In the example below we set the correct Return-Path header for sendmail. Normally sendmail will add the X-Authentication-Warning header when using the -f parameter, because the webserver user is probably not a member of the trusted users. To suppress this warning, you should add the web server user to the trusted users in your sendmail config file.

Sending mail with extra headers and setting an additional command line parameter. mail("nobody@aol.com", "the subject", $message, "From: webmaster@$SERVER_NAME", "-fwebmaster@$SERVERNAME");
You can also use fairly simple string building techniques to build complex email messages.
Sending complex email.
/* recipients */ $recipient .= "friend_name " . ", " ; //note the comma
$recipient .= "next_friend " . ", ";
$recipient .= "onemore@greatsite.net";
/* subject */
$subject = "Checking PHP mail";
/* message */
$message .= "This is a test mail \n";
/* you can add a stock signature */
$message .= "-\r\n"; //Signature delimiter
/* additional header pieces for errors, From cc's, bcc's, etc */
$headers .= "From: The great webmaster \n";
$headers .= "To-Sender: \n";
$headers .= "X-Mailer: PHP\n"; // mailer
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: \n"; // Return path for errors

/* If you want to send html mail, uncomment the following line */

// $headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$headers .= "cc:birthdayarchive@php.net\n"; // CC to
$headers .= "bcc:birthdaycheck@site.net, birthdaygifts@site.net\n"; // BCCs to
/* and now mail it */
mail($recipient, $subject, $message, $headers);


Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.