Page 1 of 1

Email with Attachments

Posted: Sun Nov 16, 2008 4:30 am
by iceangel89
how do i create an email with attachments? i read this on http://sg2.php.net/function.mail

Code: Select all

<?php
$to      = $_POST['to'];
$email   = $_POST['email'];
$name    = $_POST['name'];
$subject = $_POST['subject'];
$comment = $_POST['message'];
 
$To          = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName    =strip_tags($name);
$FromEmail   =strip_tags($email);
$Subject     =strip_tags($subject);
 
$boundary1   =rand(0,9)."-"
.rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$boundary2   =rand(0,9)."-".rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
 
 
for($i=0; $i < count($_FILES['youfile']['name']); $i++){
if(is_uploaded_file($_FILES['fileatt']['tmp_name'][$i]) &&
   !empty($_FILES['fileatt']['size'][$i]) &&
   !empty($_FILES['fileatt']['name'][$i])){
    
$attach      ='yes';
$end         ='';
 
   $handle      =fopen($_FILES['fileatt']['tmp_name'][$i], 'rb');
   $f_contents  =fread($handle, $_FILES['fileatt']['size'][$i]);
   $attachment[]=chunk_split(base64_encode($f_contents));
   fclose($handle);
 
$ftype[]       =$_FILES['fileatt']['type'][$i];
$fname[]       =$_FILES['fileatt']['name'][$i];
}
}
 
/***************************************************************
 Creating Email: Headers, BODY
 1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
 ***************************************************************/
#---->Headers Part
$Headers     =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="$boundary1"
AKAM;
 
#---->BODY Part
$Body        =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="$boundary1"
 
This is a multi-part message in MIME format.
 
--$boundary1
Content-Type: text/plain;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
 
$TextMessage
--$boundary1
Content-Type: text/html;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
 
$HTMLMessage
 
--$boundary1--
AKAM;
 
/***************************************************************
 2- HTML Email WIth Multiple Attachment <<----- Attachment ------
 ***************************************************************/
 
if($attach=='yes') {
 
$attachments='';
$Headers     =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="$boundary1"
AKAM;
 
for($j=0;$j<count($ftype); $j++){
$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype[$j];
    name="$fname[$i]"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="$fname[$j]"
 
$attachment[$j]
 
ATTA;
}
 
$Body        =<<<AKAM
This is a multi-part message in MIME format.
 
--$boundary1
Content-Type: multipart/alternative;
    boundary="$boundary2"
 
--$boundary2
Content-Type: text/plain;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
 
$TextMessage
--$boundary2
Content-Type: text/html;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
 
$HTMLMessage
 
--$boundary2--
 
$attachments
--$boundary1--
AKAM;
}
 
/***************************************************************
 Sending Email
 ***************************************************************/
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";
?>
and http://www.drquincy.com/resources/tutor ... chmentphp/

but what i dont get is all the boundary and content-type stuff - there are afew in the actual message and headers. so which is for what? and is there a reference for Content-types?

Re: Email with Attachments

Posted: Sun Nov 16, 2008 4:54 am
by requinix
So is there a problem with it? Or are you simply curious?

MIME types are like file extensions but more accurate. It's a way of identifying something based on it's contents rather than it's extension.
For email there's no extension so they use MIME instead. HTML pages are text/html, regular text files are text/plain. There are types for images, and sound, and everything else. There's also a type for emails that contain attachments.

Actually, it's not just for emails. It tells whatever is reading the email that there are multiple parts to the message. Each part then has its own Content-Type. There can be a Content-Disposition (what should be done with the content), Content-Transfer-Encoding (tells how the data was encoded in the message), and a variety of others.
To tell the reader that a part has ended there are boundaries - otherwise everything would be considered content. Boundaries consist of two hyphens followed by a unique string - a string not found anywhere else in the email. At the end of a multipart email is another boundary marker, with two hyphens on the end as well to indicate that there's nothing more.

See also Wikipedia: MIME and Multipart messages.

Re: Email with Attachments

Posted: Sun Nov 16, 2008 6:55 am
by iceangel89
thanks. at first i had an issue with the code. and my email gave me an attachment w/o any name and my email text body is missing but now it works fine.

http://www.theukwebdesigncompany.com/ar ... hments.php also helped me.

thanks also for ur explaination :)