The PHP below will attach the file from the server, but will not allow me to edit the message input for the body of the email.
<?php
$fileatt = './dwrdocuments/dwr.fdf'; // Path to the file
$fileatt_type = "application/octet-sdiveam"; // File Type
$fileatt_name = date(mdy_his).'_dwr.fdf';
$email_from = $_POST['From']; // Who the email is from
$email_subject = 'DWR Submittal'; // The Subject of the email
$email_txt = $_POST['Comments']; // Message that the email has in it
$email_to = 'admin#example.com'; // 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-divansfer-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";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
header ("Location: ../confirm.html");
?>
If I do input a message for the body of the email by replacing:
$email_message . "\n\n";
the attachment does not come through, but the message will... along with some gibberish
--==Multipart_Boundary_xd05fe3fbc7afe814087a952a9c31f1fax Content-Type: application/octet-sdiveam; name="061711_080900_dwr.fdf" Content-Transfer-Encoding: base64 JUZERi0xLjIKJe+/ve+/ve+/ve+/vQoxIDAgb2JqCjw8IAovRkRGIDw8IC9GaWVsZHMgWyA8PC9U KFdvcmtfRGF0ZSkvVihramgpPj48PC9UKE1haW50ZW5hbmNlX0FyZWEpL1YoKT4+PDwvVChSb3V0 ZV9OdW1iZXIpL1YoKT4+PDwvVChEdWVfRGF0ZSkvVigyMDExLTA2LTE2IDIyOjI2OjAxKT4+PDwv VChSb2FkX1NlZ21lbnRfSUQpL1YoKT4+PDwvVChDcm9zc19TZWN0aW9uX1Bvc2l0aW9uKS9WKCk+ Pjw8L1QoTG9jYWxfTmFtZSkvVigpPj48PC9UKEZhY2lsaXR5X05hbWUpL1YoKT4+PDwvVChGYWNp bGl0eV9BZGRyZXNzKS9WKCk+Pl0gCi9GIChodHRwOi8vY2FnZWRuYXRpb24uY29tL2pjcy9waHAv ZHdyX2Zvcm0ucGRmKSAvSUQgWyA8ZjIxODgyZTJmOGQxOGJjYjY5OGRhYzlmNTllNzIwYWM+Cl0g Pj4gCj4+IAplbmRvYmoKdHJhaWxlcgo8PAovUm9vdCAxIDAgUiAKCj4+CiUlRU9GCg==
I would like to include a body to the email for comments and notes.
Why don't you use a mailer library? There are a lot out there, like Swiftmailer, PHPMailer or Zend_Mail. You'd save yourself the headache of dealing with the email details.
Have you tried ending headers with \r\n?
http://php.net/manual/en/function.mail.php
Related
I am using following code for sending email with attachment but the proper file is not getting attach with mail.
$UnidID = $_COOKIE['UniqueID'];
$guid = $_COOKIE['guid'];
$target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs";
$fname = getpathmail($UnidID,$guid);
$target_path = $target_path.$filname;
$fileatt_type = "application/fbf"; // File Type
$fileatt_name = $fname;
$data = $target_path;
$email_from = "EHPAdmin#fugro.in";
$email_subject = "EHP/PPP process";
$email_message = "Processed result for EHP/PPP processing";
$email_to = $_GET['Email'] ;
$headers = "From: ".$email_from;
$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_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
I'd suggest to use a library for sending eMails as it will handle all the header related stuff. Have a look at Zend_Mail. Sending attachments is as easy as
$mail = new Zend_Mail();
$at = $mail->createAttachment($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = 'test.gif';
$mail->send();
You need to put all of your multipart message into $email_message:
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: ".$email_from;
$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" .
"--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
chunk_split(base64_encode($data)) .
"--{$mime_boundary}--\n";
May be you missed this in your code
...
$file = fopen($data,'rb');
//saves content in $data itself
$data = fread($file,filesize($data));
fclose($file);
...
should work havn't executed myself.
give it a try
You should give your attachment file as:
"Content-Disposition: attachment; filename='/path/to/the/file/filename'";
I am generating pdf and sent it through email as attachment the code is given below for this i skipped some code for generating pdf (not needed).
Please check the code:
//pdf generated not given avobe code of how it comes
$pdfdoc = $pdf->Output("confirmation-".$client_code.".pdf", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = $mail_to;
$from = "From: <info#ksilbd.com>";
$subject = "Here is your attachment";
$mainMessage = "BUY/SALE CONFIRMATION";
$fileatt = $attachment;
$fileatttype = "application/pdf";
$fileattname = "confirmation-".$client_code.".pdf";
$headers = "From: $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}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
//base_64
//encoding used
//to encode data
$data = chunk_split(base64_encode($data));
//message
//concat the
//message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "EMAIL SENT SUCCESSFUL.";
}
else {
echo "There was an error sending the mail.";
The message showed email sent successful and i got email too but i failed to open the attached pdf file with the mail ....
please check the problem
You may use "phpmailer".
Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
and Enjoy it.
This php script works just fine sending my pdf file by e-mail.
The problem is the script doesn't send any message as specified in $mainMessage.
Why do that problem occur, with the script only sending the pdf file without any message?
// Settings
$name = "Name";
$email = "someome#anadress.com";
$to = "$name <$email>";
$from = "email#email.com";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt = "test.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";
$headers = "From: $from";
// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
// This attaches the 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}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
Try using \r\n instead of your \ns. Also, I would suggest using a library for sending emails, such as PHPMailer or SwiftMailer, instead.
EDIT: There appears to be an extra (or missing) " when you declare your charset (at "Content-Type: text/plain; charset=\"iso-8859-1\n" - it outputs Content-Type: text/plain; charset="iso-8859-1).
you didn't notice that you need to complete this line:
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\r\n" .
"Content-Type: text/plain; charset=utf-8 \r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
$mainMessage . "\r\n\r\n";
you are missing one "-" in "-{$mime_boundary}\r\n".
it should be as the example I wrote :3
I have a function to send an email with multiple word documents as attachments. Only problem is that no matter how many documents I send, only 1 shows up in the email, and also, it shows up as corrupted. The file path name is correct, I have tested this, but when I try to open the document from the email, Microsoft word complains about corrupted file. Somebody please tell me what I am doing wrong?
function mail_attachment_multiple($to, $subject, $message, $attachment, $from){//sends email as an attachment. attachment parameter is the path to file
$fileatt_type = "application/msword"; // File Type
$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;
$msg_txt="\n\n You have recieved a new attachment message from $from";
$email_message = "";
//loop through array: $key = filename; $value = filenamePATH
foreach($attachment as $key => $value){
$fileatt_name = $key; //name of file
$fileatt_path = $value; // file path (http://www.example.com/filePath)
$file = fopen($fileatt_path,'rb');
$temp = get_headers($fileatt_path, 1);
$file_size = $temp['Content-Length'];
$data = fread($file, $file_size); //filesize($fileatt_path)
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_txt .= $msg_txt;
$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[$key] = 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_path}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
}
return #mail($email_to, $email_subject, $email_message, $headers);?>
Thanks for the help!
You should really use a mail library for this sort of thing. I highly recommend SwiftMailer.
I have finally pieced together a code that would allow me to send an FDF file from my server to e-mail that didn't reduce my file to a 0kb attachment. Problem is, that I had to get rid of the body of the e-mail to do it.
I want to be able to add an HTML (or plain text) body to the e-mail being sent. Kind of like:
<h2>DWR Submittal</h2>
<p>Process the attachment in the system.</p>
STEP 1. Download file
STEP 2. Open with Adobe Acrobat
STEP 3. Verify form data
STEP 4. etc...
STEP 5. etc...
Where or how could this be done without compromising the attachment
<?php
$fileatt = './dwrdocuments/dwr.fdf'; // Path to the file
$fileatt_type = "application/octet-sdiveam"; // File Type
$fileatt_name = date(mdy_his).'_dwr.fdf';
$email_from = $_POST['From']; // Who the email is from
$email_subject = 'DWR Submittal'; // The Subject of the email
$email_txt = $_POST['Comments']; // Message that the email has in it
$email_to = 'admin#example.com'; // 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-divansfer-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";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
header ("Location: ../confirm.html");
?>
Try adding:
$email_message .= '<h2>DWR Submittal</h2>
<p>Process the attachment in the system.</p>
STEP 1. Download file
STEP 2. Open with Adobe Acrobat
STEP 3. Verify form data
STEP 4. etc...
STEP 5. etc...';
Right before:
$data = chunk_split(base64_encode($data));