Advanced email in PHP

This is an article discussion thread for discussing the SitePoint article, “Advanced email in PHP

i want to know whether a user read my email in php(code)

this is a gr8 article but how can we attach a file existing on the server ?

Just generate the $fileatt, $fileatt_type, $fileatt_name and $data variables from a local file rather than an uploaded one.

Also, have you consider PHPMailer ?

this is my question … how can i generate these variables from a local file ?

Change paths, I posted a quick answer in your other thread.

it gives undefined function error
plz c the link
http://www.sitepoint.com/forums/showthread.php?t=185056

Great article! Especially the mail headers part.

How do you send an email if you want to use a specific mail server, but you do not have access to the PHP web server’s settings (for example if you have shared hosting)?

if i wanna send 2 or more attachment then what should i do?

I know there is probably code already out there that does this, but I feel like I learn more when I do things myself. I built a modification for Kevin’s program that will accept up to 3 attachments. Here is the code.

index.html:

<html>
<head>
<title>Send an Email</title>
</head>
<body>
<h1>Send an Email</h1>
<form action="mail.php" method="POST" enctype="multipart/form-data">
<p>To: <input type="text" name="to" value="" /><br />
From: <input type="text" name="from" value="" /><br />
Subject: <input type="text" name="subject" value="" /></p>
<p>Message:<br />
<textarea cols="70" rows="20" name="message"></textarea></p>
<p>File Attachment 1: <input type="file" name="fileatt" /></p>
<p>File Attachment 2: <input type="file" name="fileatt2" /></p>
<p>File Attachment 3: <input type="file" name="fileatt3" /></p>
<p><input type="submit" value="Send" /></p>
</form>
</body>
</html>

mail.php:

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = stripslashes($_POST['message']);

// Obtain file upload vars
$fileatt		= $_FILES['fileatt']['tmp_name'];
$fileatt_type	= $_FILES['fileatt']['type'];
$fileatt_name	= $_FILES['fileatt']['name'];
$fileatt2		= $_FILES['fileatt2']['tmp_name'];
$fileatt_type2	= $_FILES['fileatt2']['type'];
$fileatt_name2	= $_FILES['fileatt2']['name'];
$fileatt3		= $_FILES['fileatt3']['tmp_name'];
$fileatt_type3	= $_FILES['fileatt3']['type'];
$fileatt_name3	= $_FILES['fileatt3']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt) || is_uploaded_file($fileatt2) || is_uploaded_file($fileatt3)) {
  // Read the file to be attached ('rb' = read binary)
  if (is_uploaded_file($fileatt)) {
  	$file = fopen($fileatt,'rb');
  	$data = fread($file,filesize($fileatt));
  	fclose($file);

  	// Base64 encode the file data
  	$data = chunk_split(base64_encode($data));
  }

  if (is_uploaded_file($fileatt2)) {
  	$file2 = fopen($fileatt2,'rb');
  	$data2 = fread($file2,filesize($fileatt2));
  	fclose($file2);

  	// Base64 encode the file data
  	$data2 = chunk_split(base64_encode($data2));
  }

  if (is_uploaded_file($fileatt3)) {
  	$file3 = fopen($fileatt3,'rb');
  	$data3 = fread($file3,filesize($fileatt3));
  	fclose($file3);

  	// Base64 encode the file data
  	$data3 = chunk_split(base64_encode($data3));
  }

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\
MIME-Version: 1.0\
" .
              "Content-Type: multipart/mixed;\
" .
              " boundary=\\"{$mime_boundary}\\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\
\
" .
             "--{$mime_boundary}\
" .
             "Content-Type: text/plain;\
 charset=\\"iso-8859-1\\"\
" .
             "Content-Transfer-Encoding: 7bit\
\
" .
             $message . "\
\
" .
			 "--{$mime_boundary}\
";

  // Add file attachment(s) to the message
  if ($fileatt_name > "") {
  	$message .= "Content-Type: {$fileatt_type};\
" .
         	     " name=\\"{$fileatt_name}\\"\
" .
            	  "Content-Transfer-Encoding: base64\
" .
           	     "Content-Disposition: attachment;\
" .
           	     " filename=\\"{$fileatt_name}\\"\
\
" .
            	  $data . "\
" .
            	  "--{$mime_boundary}\
";
	}
	
	
  if ($fileatt_name2 > "") {
  	$message .=  "Content-Type: {$fileatt_type2};\
" .
         	     " name=\\"{$fileatt_name2}\\"\
" .
            	  "Content-Transfer-Encoding: base64\
" .
           	     "Content-Disposition: attachment;\
" .
           	     " filename=\\"{$fileatt_name2}\\"\
\
" .
            	  $data2 . "\
" .
            	  "--{$mime_boundary}\
";
	}
	
	
  if ($fileatt_name3 > "") {
  	$message .=  "Content-Type: {$fileatt_type3};\
" .
         	     " name=\\"{$fileatt_name3}\\"\
" .
            	  "Content-Transfer-Encoding: base64\
" .
           	     "Content-Disposition: attachment;\
" .
           	     " filename=\\"{$fileatt_name3}\\"\
\
" .
            	  $data3 . "\
" .
            	  "--{$mime_boundary}";
	}
	$message .=  "--\
\
";	
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>

I am getting this error in my maillog when I use the PHP command:
Dec 29 20:58:25 www sendmail[23641]: iBU1wPd23641: ruleset=check_mail, arg1=<postmaster@localhost>, relay=win03.xxx.com [66.xx.206.210], reject=553 5.5.4 <postmaster@localhost>… Real domain name required for sender address
Dec 29 20:58:25 www sendmail[23641]: iBU1wPd23641: from=<postmaster@localhost>, size=309, class=0, nrcpts=0, proto=ESMTP, daemon=MTA, relay=win03.xxx.com [66.xx.206.210]

mail.php:
Mail sent! Yay PHP!

When I tried the sample code and had an email with attachment sent to my email, my anti-virus app Avast blocked it. “Multiple Content-Type header - HIGH DANGER!”

This has me wondering how this message differs from any other email with attachments.

Any ideas?

I cannot get this code to email an attachment! Only the file name comes over. What should I be doing. Anyone - Please help me - this is for a non for profit site and volunteer programming.

<HTML>
<BODY>
<PRE><?
/*

*/

$mailto = ‘timmytiger32@hotmail.com’ ;

$subject = “Awardee Nomination” ;

$formurl = “http://www.cast-utech.ca/awardees/nonform.html” ;
$errorurl = “http://www.cast-utech.ca/awardees/err.html” ;
$thankyouurl = “http://www.cast-utech.ca/awardees/thanks.html” ;

$name = $_POST[‘name’] ;
$address = $_POST[‘address’] ;
$city = $_POST[‘city’] ;
$province = $_POST[‘province’] ;
$postal = $_POST[‘postal’] ;
$email = $_POST[‘email’] ;
$telephone = $_POST[‘telephone’] ;
$start = $_POST[‘start’] ;
$end = $_POST[‘end’] ;
$job = $_POST[‘job’] ;
$utech = $_POST[‘utech’] ;
$prof = $_POST[‘prof’] ;
$society = $_POST[‘society’] ;
$cuaa = $_POST[‘cuaa’] ;
$community = $_POST[‘community’] ;
$other = $_POST[‘other’] ;
$comments = $_POST[‘comments’] ;

$nomname = $_POST[‘nomname’] ;
$nomadd = $_POST[‘nomadd’] ;
$nomcity = $_POST[‘nomcity’] ;
$nomprov = $_POST[‘nomprov’] ;
$nomcode = $_POST[‘nomcode’] ;
$nomemail = $_POST[‘nomemail’] ;
$nomtele = $_POST[‘nomtele’] ;

// Obtain file upload vars
$fileatt = $_FILES[‘fileatt’][‘tmp_name’];
$fileatt_type = $_FILES[‘fileatt’][‘type’];
$fileatt_name = $_FILES[‘fileatt’][‘name’];

$http_referrer = getenv( “HTTP_REFERER” );

if (is_uploaded_file($fileatt)) {
// Read the file to be attached (‘rb’ = read binary)
$file = fopen($fileatt,‘rb’);
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

// Add the headers for a file attachment
$headers .= "
MIME-Version: 1.0
" .
“Content-Type: multipart/mixed;
" .
" boundary=\”{$mime_boundary}\“”;

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

/* Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.

" .
"–{$mime_boundary}
" .
"Content-Type: text/plain; charset=\“iso-8859-1\”
" .
"Content-Transfer-Encoding: 7bit

" .
$message . "

";

*/

if (!isset($_POST[‘email’])) {
header( “Location: $formurl” );
exit ;
}
if (empty($name) || empty($email) || empty($comments)) {
header( “Location: $errorurl” );
exit ;
}
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

$messageproper =

"------------------------- AWARDEE INFORMATION -------------------------\

" .
"Name: -$name
" .
"Address: -$address
" .
"City: -$city
" .
"Province: -$province
" .
"Postal Code: -$postal
" .
"E-mail: -$email
" .
"Telephone: -$telephone
" .
"Attended Cast from: -$start
" .
"to: -$end
" .
"Profession: -$job
" .
"Contribution: -
" .
"Utech: -$utech
" .
"prof: -$prof
" .
"society: -$society
" .
"cuaa: -$cuaa
" .
"community: -$community
" .
"other: -$other
" .

"Other Information for this Nominatiom\

" .
“$comments
" .
// Add file attachment to the message
$comments .= “–{$mime_boundary}
" .
“Content-Type: {$fileatt_type};
" .
" name=\”{$fileatt_name}\”
" .
“Content-Disposition: attachment;
" .
" filename=\”{$fileatt_name}\”
" .
"Content-Transfer-Encoding: base64

" .
$data . "

" .
"–{$mime_boundary}–
";
}

"----------------------------------------------------------------------\


" .
"This message was sent from:
" .
"$http_referrer
" .
"By;
" .
"$nomname
" .
"$nomadd
" .
"$nomcity
" .
"$nomprov
" .
"$nomcode
" .
"$nomemail
" .
"$nomtele
" .

"\


-----------------------------------------------------------------------
" ;

mail($mailto, $subject, $messageproper, “From: \”$nomname\" <$nomemail>
Reply-To: \“$nomname\” <$nomemail>
X-Mailer: chfeedback.php 2.03" );
header( “Location: $thankyouurl” );
exit ;

?>
</PRE>
</BODY></HTML>

Can you add bcc to the mail?

Yes, as a matter of fact, in the Advanced email in PHP section, he does just that:

 "cc: Interested &lt;someone@some.net&gt;\

" .
"Bcc: Me Too <metoo@some.net>
" .
It is done with the optional “additional headers”

It doesn’t get much easier than that. Thanks man.

The only thing that can be improved is maybe deeper connection to php, but whattheheck.

How to send e-mail to smtp server with authorization

Well done. Thanks man for your article.