Current location: Hot Scripts Forums » Programming Languages » PHP » senting html mail with attachment in php problem


senting html mail with attachment in php problem

Reply
  #1 (permalink)  
Old 11-28-06, 12:33 PM
netlovers netlovers is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
senting html mail with attachment in php problem

I have found the following code for sending html email with attachment .
This code is working fi9 but it is not showing message text "hello" defined in $email_txt

PHP Code:

<?

if($_FILES["fileatt"] != NULL)
{
    
$fileatt_type "application/octet-stream"//File Type
    
$email_from "baber@inbox.com"// Who the email is from
    
$email_subject "Test File Attach"// The Subjectof the email
    
$email_txt "hello";       // Message that the email has in it

    
$email_to "baberzubair@gmail.com"// Who the email is too

    
$headers "From: ".$email_from;

    
$file fopen($_FILES["fileatt"]["tmp_name"],"rb");
    
$data =
fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
    
fclose($file);

    
$semi_rand md5(time());
    
$mime_boundary "==Multipart_Boundary_x{$semi_rand}x";

    
$headers .= "\nMIME-Version: 1.0\n" .
    
"Content-Type: multipart/mixed;\n" .
    
" boundary=\"{$mime_boundary}\"";

    
$email_message .= "This is a multi-partmessage in
MIME format.\n\n" 
.
    
"--{$mime_boundary}\n" .
    
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    
"Content-Transfer-Encoding: 7bit\n\n" .
    
$email_message "\n\n";

    
$data chunk_split(base64_encode($data));

    
$email_message .= "--{$mime_boundary}\n" .
    
"Content-Type: {$fileatt_type};\n" .
    
" name=\"{$fileatt_name}\"\n" .
    
"Content-Disposition: attachment;\n" .
    
" filename=\"{$fileatt_name}\"\n" .
    
"Content-Transfer-Encoding: base64\n\n" .
    
$data "\n\n" .
    
"--{$mime_boundary}--\n";
    
$email_message $email_message.$email_txt;

    
$ok = @mail($email_to$email_subject,
    
$email_message$headers);

    if(
$ok)
    {
        echo 
"<font face=verdana size=2>The file was
successfully sent!</font>"
;
    }else{
        die(
"Sorry but the email could not be sent.
Please go back and try again!"
);
    }
}
?>
so if u have solution for this problem plz tell me.

Last edited by Nico; 11-28-06 at 12:38 PM. Reason: Please use [php] wrappers when posting PHP code.
Reply With Quote
  #2 (permalink)  
Old 11-28-06, 01:10 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
that's because you enter the mail_txt outside the mail boundaries. You should first assign the $email_txt to $email_message before closing the boundary "--{$mime_bounday}--\n"

I would recommend you to use phpmailer or another class. These allow you to send multipart messages easily.

UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 11-28-06, 03:29 PM
netlovers netlovers is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Still not working

I tried assigning the email_txt before closing the boudries but it doesnot work.so you plz copy whole of code and then rewrite after making nessesary changes so i will copy the whole code.
Reply With Quote
  #4 (permalink)  
Old 11-29-06, 07:10 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
PHP Code:

<?

if($_FILES["fileatt"] != NULL)
{
    
$fileatt_type "application/octet-stream"//File Type
    
$email_from "baber@inbox.com"// Who the email is from
    
$email_subject "Test File Attach"// The Subjectof the email
    
$email_txt "hello";       // Message that the email has in it

    
$email_to "baberzubair@gmail.com"// Who the email is too

    
$headers "From: ".$email_from;

    
$file fopen($_FILES["fileatt"]["tmp_name"],"rb");
    
$data =
fread($file,filesize($_FILES["fileatt"]["tmp_name"]));
    
fclose($file);

    
$semi_rand md5(time());
    
$mime_boundary "==Multipart_Boundary_x{$semi_rand}x";

    
$headers .= "\nMIME-Version: 1.0\n" .
    
"Content-Type: multipart/mixed;\n" .
    
" boundary=\"{$mime_boundary}\"";

    
$email_message .= "This is a multi-partmessage in
MIME format.\n\n" 
.
    
"--{$mime_boundary}\n" .
    
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    
"Content-Transfer-Encoding: 7bit\n\n" .
    
$email_txt."\n\n";

    
$email_message .= "\n\n--{$mime_boundary}\n" .
    
"Content-Type: {$fileatt_type};\n" .
    
" name=\"{$fileatt_name}\"\n" .
    
"Content-Disposition: attachment;\n" .
    
" filename=\"{$fileatt_name}\"\n" .
    
"Content-Transfer-Encoding: base64\n\n" .
    
chunk_split(base64_encode($data)) . "\n\n" .
    
"--{$mime_boundary}--\n";
    
$email_message $email_message;

    
$ok = @mail($email_to$email_subject,
    
$email_message$headers);

    if(
$ok)
    {
        echo 
"<font face=verdana size=2>The file was
successfully sent!</font>"
;
    }else{
        die(
"Sorry but the email could not be sent.
Please go back and try again!"
);
    }
}
?>
I think this should do it. But once again, i think it's better to use existing classes to creata multipart messages. phpmailer is one of them

UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for php mail form w/ attachments and required fields Sithlord999 Script Requests 0 08-11-06 04:14 AM
how can i stop my PHP mail going to BULK ? sham85131 PHP 2 03-06-06 02:43 PM
How to Make my Php output write static Html files cebuy PHP 1 02-04-05 05:52 AM
Putting PHP into a HTML page? PigeonHead HTML/XHTML/XML 5 01-24-05 02:22 PM
Can PHP place quotes around HTML attributes frdmsun PHP 1 08-17-04 02:12 AM


All times are GMT -5. The time now is 05:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.