Use PHP Code to Send Email
Yesterday, I was assigned an task of creating a contest registration page so that when people register, I should get their information and at the same time, the person who register should get an welcome email from me. How can I accomplish that by php? I know it is a very easy job for a php expert. For me, I am just like all you guys here - a beginner! I start to recall the functions and did some research on the web and finally, I get it done! Please look at the following code and explanations for this email sending php code:
First, I think, if I want to send them a welcome email, which is store in my server called "welcome-mail,html", I should first, tell php find, open and then read the file. So I start by writing the following code:
// I assigned the filename to a variable to tell php where my file is stored
$file = 'welcome-mail.html';
// I am telling php to open the file by using the build-in function in php - "fopen"
$fh = fopen($file, 'r') or die('Could not open file!');
// Tell php to read the file it opend in the last step and then assign a variable $data to content of the file
$data = fread($fh, filesize($file)) or die('Could not read file!');
//After reading the content, close the file by using function "fclose"
fclose($fh);
Then, I need to retrive the information about my registered user:
$fname = trim($_POST['fname']); //Get the first name of register
$lname = trim($_POST['lname']); //Get the last name of register
$mail = trim($_POST['email']); //Get the email of register
$site = trim($_POST['website']); //Get the web address of register
$subject="Thank you for your registration"; // the subject that the participator see
$subject_main="We get another one to join, Rock!"; // the subject I see
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ; //assign the content of the file to a new variable - "$mailmessage"
Then I am telling php to send the following to different email address, which is to say, send an email to me that tells me there is another person who wants to join the contest and at the same time, send an welcome mail to that new participator.
// Additional headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.
We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings. ";
To sum up, here is the whole php code:
$file = 'matt-email.html';
$fh = fopen($file, 'r') or die('Could not open file!');
$data = fread($fh, filesize($file)) or die('Could not read file!');
fclose($fh);
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$mail = trim($_POST['email']);
$site = trim($_POST['website']);
$subject="Thank you for your registration";
$subject_main="We get another one to join";
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";
// send an html mail...
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.
We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings.";
?>
First, I think, if I want to send them a welcome email, which is store in my server called "welcome-mail,html", I should first, tell php find, open and then read the file. So I start by writing the following code:
// I assigned the filename to a variable to tell php where my file is stored
$file = 'welcome-mail.html';
// I am telling php to open the file by using the build-in function in php - "fopen"
$fh = fopen($file, 'r') or die('Could not open file!');
// Tell php to read the file it opend in the last step and then assign a variable $data to content of the file
$data = fread($fh, filesize($file)) or die('Could not read file!');
//After reading the content, close the file by using function "fclose"
fclose($fh);
Then, I need to retrive the information about my registered user:
$fname = trim($_POST['fname']); //Get the first name of register
$lname = trim($_POST['lname']); //Get the last name of register
$mail = trim($_POST['email']); //Get the email of register
$site = trim($_POST['website']); //Get the web address of register
$subject="Thank you for your registration"; // the subject that the participator see
$subject_main="We get another one to join, Rock!"; // the subject I see
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ; //assign the content of the file to a new variable - "$mailmessage"
Then I am telling php to send the following to different email address, which is to say, send an email to me that tells me there is another person who wants to join the contest and at the same time, send an welcome mail to that new participator.
// Additional headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.
We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings. ";
To sum up, here is the whole php code:
$file = 'matt-email.html';
$fh = fopen($file, 'r') or die('Could not open file!');
$data = fread($fh, filesize($file)) or die('Could not read file!');
fclose($fh);
$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$mail = trim($_POST['email']);
$site = trim($_POST['website']);
$subject="Thank you for your registration";
$subject_main="We get another one to join";
$name=$fname.' '.$lname;
$mailmessage="\r\n Name: ".$name."\r\n Website: ".$site."\r\n Email Address: ".$mail;
$mailmessage=$data ;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: '.$fname.' '.$lname.' <'.$mail.'>' . "\r\n";
$headers .= 'From: Jeff Cai' . "\r\n";
// send an html mail...
mail('mymail.com', $subject_main, $message, $headers);
mail($mail, $subject, $mailmessage, $headers);
echo "
Thank you for your registration! You will receive an email from us shortly.
We will provide you tips on how you can make your chances of winning even better and keep you updated with our contest standings.";
?>