Connecting Tech Pros Worldwide Forums | Help | Site Map

mail with file attachment

Member
 
Join Date: Aug 2006
Posts: 110
#1: Mar 12 '07
Dear Sir,

I have created a simple request form which will be mailed to me.

Now I want to attach files and send it through that request form.

For this I am using the following script:

[PHP]<?php
function mail_attachment ($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment

$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email

$email_txt = $message; // Message that the email has in it

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
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-part message 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";

$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";


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

if($ok) { echo "yes";//header('Location:newrequestsubmit_y.html');
} else {
header('Location:newsubmit_n.html');
}
}

?>[/PHP]


In the below coding I am calling the mail_attachment function.


[PHP]<?
include 'mail_attachment.php';

if (!$_POST['newproject'])
$errors[] = "Project name is required";
else
$newproject = $_POST['newproject'];
// if there are any errors, display them
if (count($errors)>0) {
foreach($errors as $err)
echo "$err<br>\n";
echo "<br>Please use your browser's Back button to fix.";
} else {
// no errors, so we build our message

$to = "designhd@sitanet.com";
$subject = "Your request";
$from = stripslashes($_POST['email']);

$msg = "Sendor Information\n";
$msg.= "\nName: ".$_POST['fname']." ".$_POST['lname'];
$msg.= "\nOffice: ".$_POST['office'];
$msg.= "\nEmail: ".$_POST['email'];
$msg.= "\nExtn.: ".$_POST['extn'];

$msg.= "\n\nProject name: ".$newproject;
$msg.="\nMedia type: ".$_POST['media'];

switch($_POST['project_type']){
case 'web_banner':
$proj_type = "Web Banner";
break;
case 'print_banner':
$proj_type = "Print Banner";
break;
case 'flyer':
$proj_type = "Flyer";
break;
case 'invitation':
$proj_type = "Invitation";
break;
case 'program':
$proj_type = "Program";
break;
case 'other':
$proj_type = $_POST['other'];
break;
default:
$proj_type = " ";
}


$msg.="\nProject type: ".$proj_type;
$msg.="\nSize: Height = ".$_POST['height']." ".$_POST['dimension'].", Width = ".$_POST['width']." ".$_POST['dimension'];
$msg.="\nColor: ".$_POST['color'];

switch($_POST['month']){
case '1':
$month = "January";
break;
case '2':
$month = "February";
break;
case '3':
$month = "March";
break;
case '4':
$month = "April";
break;
case '5':
$month = "May";
break;
case '6':
$month = "June";
break;
case '7':
$month = "July";
break;
case '8':
$month = "August";
break;
case '9':
$month = "September";
break;
case '10':
$month = "October";
break;
case '11':
$month = "November";
break;
case '12':
$month = "December";
break;
default:
$month = " ";
}




$msg.="\nDue date: ".$_POST['date']." ".$month.", ".$_POST['year'];
$msg.="\nTime by: ".$_POST['hours']." Hrs ".$_POST['minutes']." Mins".$_POST['standard'];
$msg.="\nDescription: ".stripslashes($_POST['description'])."\n";
$msg.="\nNumber of files: ".$_POST['files'];
$msg.="\nQuality/Resolution of files: ".$_POST['quality'];


$pdf=($_POST['PDF'])?"PDF: Yes":"PDF: No";
$jpg=($_POST['JPG'])?"JPG: Yes":"JPG: No";
$gif=($_POST['GIF'])?"GIF: Yes":"GIF: No";
$psd=($_POST['PSD'])?"PSD: Yes":"PSD: No";
$others=($_POST['OTHERS'])?"OTHERS: Yes".$_POST['out_format']:"OTHERS: No";

$msg.="\n\nOutput format:\n\n$pdf\n$jpg\n$gif\n$psd\n$others\n";



mail_attachment($from, $to, $subject, $msg, 'mail_attachment.php');
}
?>[/PHP]


1. Now.....When I submit the form the attachment file comes to me with 'ail_attachment.php' as you can see in coding it is 'mail_attachment.php'.

I can't understand why 'm' is cutting in the file name.

2. And second the email text is coming to me in one line constantly. It means the '\n' (line break or next line) is not working. I don't know why?


Appreciate if you can please help me.......I really really need your help.

I need two things to be done:

1. More than one file to be attach and sent.
2. Proper email message should come.



Thanks
Deepak

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 12 '07

re: mail with file attachment


These statements in your code cause the first char of the attachment name to be stripped off:
[php]$start=strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); [/php]

The eol problem could be caused by the Windows system. The following sample (I always use it) is to set the $eol variable with the end-of-line string depending on the OS. Then use the $eol variable instead of hardcoding the "\n".
[php]
# Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
}[/php]

Ronald :cool:
Member
 
Join Date: Aug 2006
Posts: 110
#3: Mar 12 '07

re: mail with file attachment


Quote:

Originally Posted by ronverdonk

These statements in your code cause the first char of the attachment name to be stripped off:
[php]$start=strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); [/php]

The eol problem could be caused by the Windows system. The following sample (I always use it) is to set the $eol variable with the end-of-line string depending on the OS. Then use the $eol variable instead of hardcoding the "\n".
[php]
# Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
}[/php]

Ronald :cool:


I really appreciate your reply sir. But it is still not working for me......the email message is still in constantly one line...Below is the example


[HTML]Sendor Information Name: Deepak Saxena Office: Onlineres Email: deepaks85@gmail.com Extn.: 18185748468 Project name: Syrian air project Media type: Web Project type: Print Banner Size: Height = 34 inch, Width = 34 inch Color: Limited colors Due date: 4 March, 2007 Time by: 5 Hrs 4 MinsIST Description: Please send to me. Number of files: 3 Quality/Resolution of files: High resolution Output format: PDF: Yes JPG: Yes GIF: No PSD: Yes OTHERS: No[/HTML]
Member
 
Join Date: Aug 2006
Posts: 110
#4: Mar 12 '07

re: mail with file attachment


Quote:

Originally Posted by deepaks85

I really appreciate your reply sir. But it is still not working for me......the email message is still in constantly one line...Below is the example


[HTML]Sendor Information Name: Deepak Saxena Office: Onlineres Email: deepaks85@gmail.com Extn.: 18185748468 Project name: Syrian air project Media type: Web Project type: Print Banner Size: Height = 34 inch, Width = 34 inch Color: Limited colors Due date: 4 March, 2007 Time by: 5 Hrs 4 MinsIST Description: Please send to me. Number of files: 3 Quality/Resolution of files: High resolution Output format: PDF: Yes JPG: Yes GIF: No PSD: Yes OTHERS: No[/HTML]

Here is the code again:



[PHP]<?
include 'mail_attachment.php';


# Is the OS Windows or Mac or Linux

if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
}




if (!$_POST['newproject'])
$errors[] = "Project name is required";
else
$newproject = $_POST['newproject'];
// if there are any errors, display them
if (count($errors)>0) {
foreach($errors as $err)
echo "$err<br>\n";
echo "<br>Please use your browser's Back button to fix.";
} else {
// no errors, so we build our message




$to = "deepaks85@gmail.com";
$subject = "Your request";
$from = stripslashes($_POST['email']);

$msg = "Sendor Information".$eol;
$msg.= $eol."Name: ".$_POST['fname']." ".$_POST['lname'];
$msg.= $eol."Office: ".$_POST['office'];
$msg.= $eol."Email: ".$_POST['email'];
$msg.= $eol."Extn.: ".$_POST['extn'];

$msg.= $eol."Project name: ".$newproject;
$msg.=$eol."Media type: ".$_POST['media'];

switch($_POST['project_type']){
case 'web_banner':
$proj_type = "Web Banner";
break;
case 'print_banner':
$proj_type = "Print Banner";
break;
case 'flyer':
$proj_type = "Flyer";
break;
case 'invitation':
$proj_type = "Invitation";
break;
case 'program':
$proj_type = "Program";
break;
case 'other':
$proj_type = $_POST['other'];
break;
default:
$proj_type = " ";
}


$msg.=$eol."Project type: ".$proj_type;
$msg.=$eol."Size: Height = ".$_POST['height']." ".$_POST['dimension'].", Width = ".$_POST['width']." ".$_POST['dimension'];
$msg.=$eol."Color: ".$_POST['color'];

switch($_POST['month']){
case '1':
$month = "January";
break;
case '2':
$month = "February";
break;
case '3':
$month = "March";
break;
case '4':
$month = "April";
break;
case '5':
$month = "May";
break;
case '6':
$month = "June";
break;
case '7':
$month = "July";
break;
case '8':
$month = "August";
break;
case '9':
$month = "September";
break;
case '10':
$month = "October";
break;
case '11':
$month = "November";
break;
case '12':
$month = "December";
break;
default:
$month = " ";
}




$msg.=$eol."Due date: ".$_POST['date']." ".$month.", ".$_POST['year'];
$msg.=$eol."Time by: ".$_POST['hours']." Hrs ".$_POST['minutes']." Mins".$_POST['standard'];
$msg.=$eol."Description: ".stripslashes($_POST['description'])."\n";
$msg.=$eol."Number of files: ".$_POST['files'];
$msg.=$eol."Quality/Resolution of files: ".$_POST['quality'];



$pdf=($_POST['PDF'])?"PDF: Yes":"PDF: No";
$jpg=($_POST['JPG'])?"JPG: Yes":"JPG: No";
$gif=($_POST['GIF'])?"GIF: Yes":"GIF: No";
$psd=($_POST['PSD'])?"PSD: Yes":"PSD: No";
$others=($_POST['OTHERS'])?"OTHERS: Yes".$_POST['out_format']:"OTHERS: No";

$msg.=$eol."Output format:\n\n$pdf\n$jpg\n$gif\n$psd\n$others\n";

mail_attachment($from, $to, $subject, $msg, 'dhd.gif');
}

?>[/PHP]
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#5: Mar 13 '07

re: mail with file attachment


I still do not know the cause but the following piece of your code (in the function) causes the eols to disappear. I commented it out and it emailed the correct formatted text with newlines. So you'll have to look inside this piece of code to pinpoint the error.

[php]
$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-part message 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";

$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"; [/php]

Ronald :cool:
Member
 
Join Date: Aug 2006
Posts: 110
#6: Mar 13 '07

re: mail with file attachment


Quote:

Originally Posted by ronverdonk

I still do not know the cause but the following piece of your code (in the function) causes the eols to disappear. I commented it out and it emailed the correct formatted text with newlines. So you'll have to look inside this piece of code to pinpoint the error.

[php]
$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-part message 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";

$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"; [/php]

Dear Sir,

I found the error by searching on net.

Below is the previous coding:

[PHP]$email_message .= "This is a multi-part message 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"; [/PHP]

Here you can see on line:

[PHP] "Content-Type:text/html; charset=\"iso-8859-1\"\n" . [/PHP]

We have to write:
[PHP] "Content-Type:text/plain; charset=\"iso-8859-1\"\n" . [/PHP]
instead of previous one.

Thanks very much for your kind cooperation sir. The file name is also coming with the full name. Your assumtion were right.

Now can you please help in that how can I attach and send more than one file?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#7: Mar 13 '07

re: mail with file attachment


Attachments with the mail() function are complex but possible. You just have to have boundaries between the various sections of the e-mail. See this sample where 2 images are to be attached. Of course you'll have to adapt it to suit the files you want to attach:
[php]// Add file attachment to the message (image attachment 1)
// Base64 encode the file data first
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" . // {$fileatt_type}
" name=\"{$fileatt_name}\"\n" .//attachment one
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";//end first attachment
//************************************************** *********
// Add file attachment to the message (image attachment 2)
// Base64 encode the file datadata first
$data2 = chunk_split(base64_encode($data2));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type2};\n" . // {$fileatt_type}
" name=\"{$fileatt_name2}\"\n" .//attachment 2
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name2}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";//end second attachment
//************************************************** ********** [/php]

Ronald :cool:
Member
 
Join Date: Aug 2006
Posts: 110
#8: Mar 15 '07

re: mail with file attachment


Quote:

Originally Posted by ronverdonk

Attachments with the mail() function are complex but possible. You just have to have boundaries between the various sections of the e-mail. See this sample where 2 images are to be attached. Of course you'll have to adapt it to suit the files you want to attach:
[php]// Add file attachment to the message (image attachment 1)
// Base64 encode the file data first
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" . // {$fileatt_type}
" name=\"{$fileatt_name}\"\n" .//attachment one
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";//end first attachment
//************************************************** *********
// Add file attachment to the message (image attachment 2)
// Base64 encode the file datadata first
$data2 = chunk_split(base64_encode($data2));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type2};\n" . // {$fileatt_type}
" name=\"{$fileatt_name2}\"\n" .//attachment 2
"Content-Disposition: inline;\n" .
" filename=\"{$fileatt_name2}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";//end second attachment
//************************************************** ********** [/php]

Thanks very much for your kind cooperation sir. After reading the script it, I think if we want to send more than one file then we will have to write script again and again for each file.

I have got a little script on the internet. Here is the link:
http://4wordsystems.com/php_mail_attachment.php

Please check it and advice me. Can I do like they mentioned in the script.

Can I have your personal email ID, if you can provide me?


Thanks
Deepak
Newbie
 
Join Date: Oct 2007
Posts: 1
#9: Oct 16 '07

re: mail with file attachment


There is no need of 11 th line. ie the MIME boundary..... for sending multiple attachments.....
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Oct 16 '07

re: mail with file attachment


Quote:

Originally Posted by dixonmd

There is no need of 11 th line. ie the MIME boundary..... for sending multiple attachments.....

Agreed, but it makes the explanatory code better structured and more readable.

Ronald
Reply