wdvltalk Roundup May 2002 - Page 22
June 4, 2002
How do we send HTML mail through a PHP script? I want to send the
processed input of a form to the user if the user enters an email
along with the other data.
-
The first place to look when you want to find out and understand anything
about PHP is the PHP site at
http://www.php.net. If you click on
"documentation" and then choose a language, you will find not only a
description of the mail( ) function but also many useful comments and
references from other users which help you do stuff like creating and adding
MIME attachments. Webmonkey (of course) also has useful tutorials...
Briefly - you call the function like this
$result = mail ($to, $subject, $message [, $additional_headers] )
Multiple recipients can be specified by putting a comma between each address
in "to", other items like "From" or "cc:" go in the additional headers, e.g.
mail ( "him@his_isp.com",
"A test message",
$test_message,
"From: me@my_isp.com\r\n"
. "Reply-To: me@my_isp.com\r\n"
. "cc: her@her_isp.com\r\n" )
-
mail.php3
Send E-mail automatically from your site using the built-in mail() function
of PHP3. The body of the e-mail will be taken from a text file that you
create. Easily modified. Source code will be mailed to you.
<?
//Go to http://www.dells.com/mail/example/send_mail.htm to see
//this code in action. The source code and documentation
//will be E-mailed to you.
//
//This code snipet will open and read the contents of
//a text file into a string variable called $body.
//It then formats headers and sends out an E-mail
//message using the built-in mail() function in PHP3.
//
//The body of the E-mail message will be identical
//to the contents of a text file you create. As this
//snipet is written, the text file must be located in
//the same directory as the mail.php3 script. It must be
//strictly ASCII text without control codes of any kind.
//However, with a little bit of experimentation, you
//should be able to send out HTML mail as well. I have not
//tested that however.
//
//On Windows, make the txt file with Notepad and turn off
//Word Wrap. On UNIX/BSD use Pica to make the txt file.
//
//The PHP3 mail() function will send the E-mail to the
//contents of variable $email. This script is expecting
//this variable to be passed from an HTML form that has
//a text input named "email" on it. The HTML forms method
//is "post" and use the path and file name of this script
//as the "action" attribute.
//
//Of course, you can always wrap this code in a function
//and use it anyplace in your own code to send E-mail in
//the background.
$textfile = "send_mail.txt"; //This is the name of the txt file you wrote
$mailsubject = "Sending E-Mail via PHP3";
$mailheaders = "From: studioq@chorus.net\n";
$mailheaders .= "Cc: \n";
$mailheaders .= "Bcc: studioq@chorus.net\n";
$mailheaders .= "Reply-To: studioq@chorus.net\n";
$mailheaders .= "X-Mailer: PHP3 Mail Function on Apache/BSD\n";
$mailheaders .= "X-Anything: isn't this cool?";
$msg = "Thank you $fname $lname for requesting this script!\n";
$msg .= "We hope it meets your needs.\n";
//open the txt file and assign the file handle
//to variable $fhandle
//then read the text file into variable $msg
//and close the txt file
//IMPORTANT: be certain that you close any file you
//open or you will cause massive memory use!
if($fhandle = fopen($textfile, "r")) {
$body = fread($fhandle,filesize($textfile));
fclose($fhandle); //CLOSE THE FILE
} else {
print("Error: the txt file could not be opened or read.");
}
//Send the mail
$msg .= $body;
if (mail($email, $mailsubject, $msg, $mailheaders)) {
print("You will receive an E-mail message from studioq@chorus.net \n");
print("Please let us know if you used this script and if it meets your
needs.\n");
}
else {
print("Error: The message could not be sent.\n");
}
?>
-
Thank you everybody :-). I was able to do it using the following
lines:
<?
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: TripInsuranceStore\r\n";
$headers .= "Reply-To:
stevedasseos@tripinsurancestore.com\r\n";
$subject="Qutation from TripInsuranceStore.com";
$mailresult=mail($email, $subject, $ft, $headers);
if($mailresult==true)
{
echo "<h1>Mail Sent!</h1>";
}
else
{
echo "<h1>Email Failure</h1>";
}
?>
where $ft contains the complete HTML coded page that I wanted to
send. It works.
Looking for advice on good beginner XML editors.
wdvltalk Roundup May 2002 - Page 21
wdvltalk Roundup
wdvltalk Roundup June 2002 - Page 23
|