I've seen the other questions/answers in StackOverflow but I cannot for the life of me get this to work correctly, and I'm really curious to see what I'm doing wrong.
An insurance company has PDFs that they'd like their customers to fill online and submit via their site. The PDF form will be sent via email as a PDF attachment all filled out.
I have a PHP script that sends the PDF, but it's not saving form data (names, addresses, etc.). It's only sending the blank PDF. Here's the code:
<?php
$fileatt = "file-name.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "file-name.pdf";
$email_from = "From"; // Who the email is from
$email_subject = "Form"; // The Subject of the email
$email_message = "Text.";
$email_to = "someemail#gmail.com"; // Who the email is to
$headers = "De: ".$email_from;
//no need to change anything else under this point
$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_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);
if($ok) {
echo 'Sent...';
//unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs
}
else {
echo 'Error...';
}
?>
I appreciate any input/feedback. I've thought about using PHPMailer but I think this is like 99% done and I'm curious to see what it's missing?
(The PDF file was created with Word then using Adobe Acrobat Pro all the input fields and the submit button was created, the submit button points to the PHP script that sends the PDF)
Related
I have this script that it was posted a while back and it works, but ...
In Safari works find (goes through the browser) not problem.
FireFox & Opera forces you to open Acrobat reader. When you submit from Acrobat Reader (stand along) the php script submits sends the email with attachment but Acrobat Reader hangs "receiving data..." then times out. Email with attachment work fine.
Chrome opens within browser submits the pdf but stays on the pdf page does not redirect. The attachment send from Chrome comes in the email but it is empty, the pdf file does not open.
My question is: What is Acrobat Reader waiting to receive? and What is going on with Chrome?
Here is the code
$fileatt = date("d-m-Y-His") . ".pdf"; // Creates unique PDF name from the date
copy('php://input',"pdfs/".$fileatt); // Copies the pdf form data to a folder named pdfs
$fileatt = "pdfs/".$fileatt; // Path to the file gives the pdfs folder plus the unique file name we just assigned
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "Application Form_".$fileatt.".pdf"; // Filename that will be used for the file as the attachment when it is sent
$email_from = "mywebsite"; // Who the email is from
$email_subject = "Completed online Applications"; // The Subject of the email
$email_message = "Please find a recent online application attached.
";
$email_message .= "Any problems please email me...
"; // Message that the email has in it
$email_to = "youremail#yourserver.com"; // Who the email is to
$headers = "From: ".$email_from;
//no need to change anything else under this point
$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_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);
if($ok) {
unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs
Header("Location: nextpage.php"); //where do we go once the form has been submitted.
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
Acrobat Reader is waiting for data... any data. Echo something which acknowledges receipt of the form. Chrome is sending an FDF, not a PDF. I don't think the attachment would actually be empty, but it's got to be able to open the PDF referred to in the FDF.
can anyone give me some idea how to attach a file from the server and send it as attachment through email??
i have the following code:
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$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" .
$message . "\n\n";
// Base64 encode the file data
// Add file attachment to the message
$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";
}
// Send the message
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
but this code does not attach file from the server.
please help me out.. thanks in advance..!!
One question: Do you want to send e-mails with attachment or do you want to improve your PHP skills with this problem?
If you only want to send mails, have a look at PHPMailer which is very good and easily to handle. For myself I'm using this for years without problems.
For improving your skills: You say that the attachment is encoded in base64, but I cannot find the line where you do something like $data = base64_encode($data); You add the $data of your attachment in plain to the mail.
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));
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