SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Mail

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("anyuserid@example.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 heade

mail("nobody@example.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 <mary@sitename.com>" . ", " ; //note the comma 
$recipient .= "next_friend <second@anothersite.com>" . ", ";
$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: <birthday@sitename.net>\n";
$headers .= "X-Mailer: PHP\n"; // mailer
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: <name@site.net> \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:userid_someone@sitename.net \n"; // CC to 
$headers .= "bcc:userid_onemore@sitename.net, someone@example.com \n"; // BCCs to 
/* and now mail it */
mail($recipient, $subject, $message, $headers);

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer