Page 1 of 1

email with attachment

Posted: Thu Sep 08, 2011 12:18 pm
by prue_
HI, got this code already.. problem is it only attached the first file. I tried putting //*** Attachment ***// code twice but getting error headers already sent. can someone help me with this.. thank you.

html form
<table><tr><td><font class="field">Upoad1 </font></td><td><input type="hidden" name="MAX_FILE_SIZE" value="500" /><input type="file" accept="image/gif, image/jpeg" name="upload1"></td>
</tr>
<tr>
<td><font class="field">upload2 </font></td><td><input type="hidden" name="MAX_FILE_SIZE" value="500" /><input type="file" accept="image/gif, image/jpeg" name="upload2">
</td></tr></table>

php

Code: Select all

	$strSid = md5(uniqid(time()));
	$strHeader = "";

	$strHeader .= "MIME-Version: 1.0\n";
	$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
	$strHeader .= "This is a multi-part message in MIME format.\n";

	$strHeader .= "--".$strSid."\n";
	$strHeader .= "Content-type: text/html; charset=utf-8\n";
	$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
	$strHeader .= $messagebody."\n\n";
	
	//*** Attachment ***//
	if($_FILES["upload1"]["name"] != "")
	{
		$strFilesName = $_FILES["upload1"]["name"];
		$strContent = chunk_split(base64_encode(file_get_contents($_FILES["upload1"]["tmp_name"]))); 
		$strHeader .= "--".$strSid."\n";
		$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
		$strHeader .= "Content-Transfer-Encoding: base64\n";
		$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
		$strHeader .= $strContent."\n\n";
	}

Re: email with attachment

Posted: Thu Sep 08, 2011 1:24 pm
by twinedev
It's been a while since I did this by hand (I use phpMailer for anything that needs that now), but I believe the answer is that you need a unique separator ($strSid) for each section. I may be wrong, but that may be it.

Using phpMailer does make it easier, and they have solid code for everything you need. You can DL it here (http://sourceforge.net/projects/phpmail ... er%20v5.1/ ) Although it looks like a ton of things in it, all you need to actually put on your site is the class.phpmailer.php file. The examples directory in that download has good example of how to use it for various situations.

-Greg

Re: email with attachment

Posted: Thu Sep 08, 2011 1:37 pm
by prue_
uh oh.. there's a lot of file in there. don't know where to start.

Re: email with attachment

Posted: Thu Sep 08, 2011 3:55 pm
by ok
You can also use Zend Framework. You don't need the whole framework, but you might want to start with the whole thing and after you get used to it, remove the unnecessary stuff.

The framework can be downlaoded from here:
http://framework.zend.com/download/latest

The instructions on how to use Zend_Mail can be found here:
http://framework.zend.com/manual/en/zend.mail.html

Re: email with attachment

Posted: Thu Sep 08, 2011 11:44 pm
by prue_
thank you for your replies. but I already got the script working except adding another file upload.. can you help me with the code above? thanks

Re: email with attachment

Posted: Fri Sep 09, 2011 12:12 am
by ok
Please post the complete code, including the call to the mail function.

Re: email with attachment

Posted: Fri Sep 09, 2011 12:51 am
by prue_
here's the entire php .. I didn't paste the html form, but there will be 2 file upload there.. please help me, for I spent days already to figure this out.

code removed

Re: email with attachment

Posted: Fri Sep 09, 2011 6:22 am
by ok
First of all, you might want to hide your private key and other private stuff...

If I am not mistaken, you need to append the files to the message body ($messagebody) and not to the headers ($strHeader) like you are doing right now.

In the PHP manual for mail() there is a comment with the following function:

Code: Select all

<?php
function multi_attach_mail($to, $files, $sendermail){
    // email fields: to, from, subject, and so on
    $from = "Files attach <".$sendermail.">"; 
    $subject = date("d.M H:i")." F=".count($files); 
    $message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
    $headers = "From: $from";
 
    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
 
    // preparing attachments
    for($i=0;$i<count($files);$i++){
        if(is_file($files[$i])){
            $message .= "--{$mime_boundary}\n";
            $fp =    @fopen($files[$i],"rb");
        $data =    @fread($fp,filesize($files[$i]));
                    @fclose($fp);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
            "Content-Description: ".basename($files[$i])."\n" .
            "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $sendermail;
    $ok = @mail($to, $subject, $message, $headers, $returnpath); 
    if($ok){ return $i; } else { return 0; }
}
Try to use the above function in your code.

Re: email with attachment

Posted: Sun Sep 11, 2011 12:19 pm
by prue_
Hi ok, thank you fro reply, tried it already but it didn't attached anything. can you help me on where to put it exactly. thank u for your reminder too.. I just removed my codes forgot that there were private stuffs there.. thank you, hope you could still help me..

Re: email with attachment

Posted: Sun Sep 11, 2011 12:31 pm
by ok
Put the function I posted in my last post in your PHP file, and call it where you want to send an email.
The function will take care of everything. However, you will need to modify the subject and message variables to your needs.

For start, try to use the function as is, without changing anything. After you test it and it works, change the subject and message variables.

Just to help you getting started:
[text]
$to - String, the email address to send the mail to
$files - Array of file paths to attach to the mail
$sendermail - String, the email address which will be used as the sender
[/text]

Re: email with attachment

Posted: Sun Sep 11, 2011 2:15 pm
by prue_
thanks got a question here $files.. how can I do this, I have two file upload in my html form.. how can I call it? thanks.. thank you for your patience :)

Re: email with attachment

Posted: Sun Sep 11, 2011 2:20 pm
by ok
You just need to copy the "tmp_name" for each file upload into the $files array.

I mean something like this:

Code: Select all

$files = array();
foreach ($_FILES as $fileUpload) {
    $files[] = $fileUpload['tmp_name']
}

Re: email with attachment

Posted: Sun Sep 11, 2011 8:14 pm
by prue_
Hi ok, it's not working, maybe I've done it the wrong way so I pasted the entire code here for I'm not good with this.. can you check? thank you once again.

code removed

Re: email with attachment

Posted: Mon Sep 12, 2011 12:13 am
by ok
You need to put your HTML before the attachments:

Code: Select all

function multi_attach_mail($email_from, $files, $email_to){
    // email fields: to, from, subject, and so on
    $from = "Files attach <".$email_to.">"; 
    $subject = date("d.M H:i")." F=".count($files); 
    // -------------------------------------------------------------------
    $message = $yourHTML;
    // -------------------------------------------------------------------
    $headers = "From: $from";
 
    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
 
    // preparing attachments
    for($i=0;$i<count($files);$i++){
        if(is_file($files[$i])){
            $message .= "--{$mime_boundary}\n";
            $fp =    @fopen($files[$i],"rb");
            $data =    @fread($fp,filesize($files[$i]));
            @fclose($fp);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
            "Content-Description: ".basename($files[$i])."\n" .
            "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
    $message .= "--{$mime_boundary}--";

    $returnpath = "-f" . $email_to;
    $ok = @mail($email_from, $subject, $message, $headers, $returnpath); 
    if($ok){ return $i; } else { return 0; }
}

Re: email with attachment

Posted: Mon Sep 12, 2011 2:02 am
by prue_
it's returning a 504 error :(