webosk
4/12/2013 - 11:50 AM

send a html email with or without an attachment

send a html email with or without an attachment

  private function send_email($name, $email, $to_mail, $subject, $msg, $attachment = "") {
    $sending = true;

    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><script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);</script>
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
		<title>\".$subject.\"</title>
		</head>
		<body> 
		\".$msg.\"<br />
		<script type="text/javascript">if (!NREUMQ.f) { NREUMQ.f=function() {
NREUMQ.push(["load",new Date().getTime()]);
var e=document.createElement("script");
e.type="text/javascript";
e.src=(("http:"===document.location.protocol)?"http:":"https:") + "//" +
  "d1ros97qkrwjf5.cloudfront.net/42/eum/rum.js";
document.body.appendChild(e);
if(NREUMQ.a)NREUMQ.a();
};
NREUMQ.a=window.onload;window.onload=NREUMQ.f;
};
NREUMQ.push(["nrfj","beacon-3.newrelic.com","5fea191aea","2478111","dVdXRUsJXllcERtQEl8XXlhKEh1HWBQ=",12,128,new Date().getTime(),"","","","",""]);</script></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;
         
        if (!empty($attachment['tmp_name'])) {
            $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."--";
        }

        if (mail($tosend['email'], $tosend['subject'], $tosend['message'] , $tosend['headers']))
            return true;
        else
            return false;
    }//-- if ($sending)
    return false;
	}