note 83825 added to ref.mail
| From: | me at sallyahmed dot com | Date: | Fri, 13 Jun 2008 16:26:47 +0000 |
| Subject: | note 83825 added to ref.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140415@lists.php.net to get a copy of this message | ||
I have fixed the file attachment with any file type function using the proper set of \r\n
terminator, as stated above email blocks are very sensitive and care should be taken when composing
attachment... the function below has been tested with xls xlsx excel files zip, pdf, doc just set
the appropriate filetype attribute and send it along with the other information to the function...
function send_mail($to,$cc, $body, $subject, $fromaddress, $fromname, $attachments,$filetype)
{
$eol="\r\n";
$mime_boundary=md5(time());
# Common Headers
$headers .= "From: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Reply-To: ".$fromname."<".$fromaddress.">".$eol;
$headers .= "Return-Path:
".$fromname."<".$fromaddress.">".$eol; // these two to set reply
address
$headers .= "To: ".$to.$eol; // these two to set reply address
$headers .= "Cc: ".$cc.$eol; // these two to set reply address
$headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid
spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/mixed;
boundary=\"".$mime_boundary."\"".$eol.$eol;
# Open the first part of the mail
$msg = "--".$mime_boundary.$eol;
$htmlalt_mime_boundary = $mime_boundary."_htmlalt"; //we must define a different MIME
boundary for this section
# Setup for text OR html -
$msg .= "Content-Type: multipart/alternative;
boundary=\"".$htmlalt_mime_boundary."\"".$eol;
# HTML Version
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol;
//close the html/plain text alternate portion
$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
# File for Attachment
$file_name = $attachments;
echo $file_name;
$handle=fopen($attachments, 'rb');
$f_contents=fread($handle, filesize($attachments));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition
using base64_encode();
$f_type=filetype($attachments);
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$filetype.";
name=\"".$file_name."\"".$eol; // sometimes i have to send MS Word, use
'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Description: ".$file_name.$eol;
$msg .= "Content-Disposition: attachment;
filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of
lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for
better security. see Injection.
# SEND THE EMAIL
//ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used
!
$mail_sent = mail($to, $subject, $msg, $headers);
//ini_restore(sendmail_from);
return $mail_sent;
}
Sally Ahmed http://www.sallyahmed.com
----
Server IP: 64.71.164.2
Probable Submitter: 212.107.116.244
----
Manual Page -- http://www.php.net/manual/en/ref.mail.php
Edit -- https://master.php.net/note/edit/83825
Del: integrated -- https://master.php.net/note/delete/83825/integrated
Del: useless -- https://master.php.net/note/delete/83825/useless
Del: bad code -- https://master.php.net/note/delete/83825/bad+code
Del: spam -- https://master.php.net/note/delete/83825/spam
Del: non-english -- https://master.php.net/note/delete/83825/non-english
Del: in docs -- https://master.php.net/note/delete/83825/in+docs
Del: other reasons-- https://master.php.net/note/delete/83825
Reject -- https://master.php.net/note/reject/83825
Search -- https://master.php.net/manage/user-notes.php