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 = "applicatio n/octet-stream"; // File Type
$start= strrpos($attach ment, '/') == -1 ? strrpos($attach ment, '//') : strrpos($attach ment, '/')+1;
$fileatt_name = substr($attachm ent, $start, strlen($attachm ent)); // 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_fr om;
$file = fopen($fileatt, 'rb');
$data = fread($file,fil esize($fileatt) );
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mi me_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(bas e64_encode($dat a));
$email_message .= "--{$mime_boundary }\n" .
"Content-Type: {$fileatt_type} ;\n" .
" name=\"{$fileat t_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fi leatt_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('Locatio n:newrequestsub mit_y.html');
} else {
header('Locatio n:newsubmit_n.h tml');
}
}
?>[/PHP]
In the below coding I am calling the mail_attachment function.
[PHP]<?
include 'mail_attachmen t.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@sitan et.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\nProjec t name: ".$newproje ct;
$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.="\nProjec t type: ".$proj_typ e;
$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.="\nDescri ption: ".stripslashes( $_POST['description'])."\n";
$msg.="\nNumber of files: ".$_POST['files'];
$msg.="\nQualit y/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\nOutp ut format:\n\n$pdf \n$jpg\n$gif\n$ psd\n$others\n" ;
mail_attachment ($from, $to, $subject, $msg, 'mail_attachmen t.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_attachmen t.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
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 = "applicatio n/octet-stream"; // File Type
$start= strrpos($attach ment, '/') == -1 ? strrpos($attach ment, '//') : strrpos($attach ment, '/')+1;
$fileatt_name = substr($attachm ent, $start, strlen($attachm ent)); // 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_fr om;
$file = fopen($fileatt, 'rb');
$data = fread($file,fil esize($fileatt) );
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mi me_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(bas e64_encode($dat a));
$email_message .= "--{$mime_boundary }\n" .
"Content-Type: {$fileatt_type} ;\n" .
" name=\"{$fileat t_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fi leatt_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('Locatio n:newrequestsub mit_y.html');
} else {
header('Locatio n:newsubmit_n.h tml');
}
}
?>[/PHP]
In the below coding I am calling the mail_attachment function.
[PHP]<?
include 'mail_attachmen t.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@sitan et.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\nProjec t name: ".$newproje ct;
$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.="\nProjec t type: ".$proj_typ e;
$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.="\nDescri ption: ".stripslashes( $_POST['description'])."\n";
$msg.="\nNumber of files: ".$_POST['files'];
$msg.="\nQualit y/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\nOutp ut format:\n\n$pdf \n$jpg\n$gif\n$ psd\n$others\n" ;
mail_attachment ($from, $to, $subject, $msg, 'mail_attachmen t.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_attachmen t.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
Comment