Page 1 of 1

PHP: Send HTML email with attachment or not

PostPosted: Tue Jan 27, 2009 11:46 pm
by weblivehelp
Hey again,

Today we give you a function that's very helpful, a function to send an email using an uploaded file as attachment (you can adapt it to use a "static" server file).

Let's cut to the chase.

Code: Select all
<?php

function sendEmail($name, $email, $to_mail, $subject, $msg, $attachment = "") {
   $sending = false;
   
   if (!empty($attachment['tmp_name']) && !empty($attachment['error'])) $attachment['tmp_name'] = "";
   
   if (!empty($name) && !empty($email) && !empty($to_mail) && !empty($subject) && !empty($msg)) {
      $from_name = $name;
      $from_mail = $email;
      $sending = true;
   }
   
   if ($sending) {
      $eol = "\n";
      
      $tosend['email'] = $to_mail;
      $tosend['subject'] = $subject;
      
      $tosend['message'] = "
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<title>".$subject."</title>
</head>
<body>
".$msg."<br />
</body>
</html>
".$eol.$eol;
      
      $tosend['headers'] = "From: \"".$from_name."\" <".$from_mail.">".$eol;
      $tosend['headers'] .= "Return-path: <".$from_mail.">".$eol;
      $tosend['headers'] .= "MIME-Version: 1.0".$eol;
      if (!empty($attachment['tmp_name'])) {
         $file = $attachment['tmp_name'];
         $content = file_get_contents($file);
         $content = chunk_split(base64_encode($content));
         $uid = md5(uniqid(time()));
         $f_name = $attachment['name'];
         $tosend['headers'] .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$uid."\"".$eol.$eol;
         $tosend['headers'] .= "This is a multi-part message in MIME format.".$eol;
         $tosend['headers'] .= "--PHP-mixed-".$uid."".$eol;
         $tosend['headers'] .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$uid."\"".$eol.$eol;
         $tosend['headers'] .= "--PHP-alt-".$uid."".$eol;
         $tosend['headers'] .= "Content-type: text/html; charset=utf-8".$eol;
         $tosend['headers'] .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
         $tosend['headers'] .= $tosend['message']."".$eol.$eol;
         $tosend['headers'] .= "--PHP-alt-".$uid."--".$eol;
         $tosend['headers'] .= "--PHP-mixed-".$uid."".$eol;
         $tosend['headers'] .= "Content-Type: application/octet-stream; name=\"".$f_name."\"".$eol; // use diff. types here
         $tosend['headers'] .= "Content-Transfer-Encoding: base64".$eol;
         $tosend['headers'] .= "Content-Disposition: attachment; filename=\"".$f_name."\"".$eol.$eol;
         $tosend['headers'] .= $content."".$eol.$eol;
         $tosend['headers'] .= "--PHP-mixed-".$uid."--";
         $tosend['message'] = "";//-- The message is already in the headers.
      }
      
      if (mail($tosend['email'], $tosend['subject'],  $tosend['message'] , $tosend['headers']))
         return true;
      else
         return false;
   }//-- if ($sending)
   return false;
}

?>


Usage:

Code: Select all
<?php

ob_start();
?>
Here goes some html message<br />
To go with the email.
<?php
$msg = ob_get_clean();

if (sendEmail("From Name", "from.email@yours.probably", "emailwhere@itgoes.to", "Email Subject", $msg, $_FILES['uploaded_file'])) {
   echo "Email sent";
} else {
   echo "Email not sent";
}

?>


So even if the uploaded file is erroneous or doesn't exist, it'll still send the html email.

Hope you enjoy it.