Bien chicos, este es mi primer hilo, y lo he buscado en línea pero sin suerte. Estoy haciendo una pasantía y estoy trabajando en un proyecto que me obliga a crear una página web que genera un archivo pdf cuando un usuario envía su información. Tan pronto como un cliente hace clic en el botón de envío, 3 cosas tienen que suceder:Enviando un correo electrónico con archivos adjuntos en PDF usando PHP
- Almacenar la información a la base de datos (hecho),
- Envía el personal de un correo electrónico con la nueva información del cliente (hecho), y
- Envíe al cliente un mensaje de "mensaje de agradecimiento" con un archivo adjunto en PDF (no funciona).
Es decir, el cliente recibirá un correo electrónico, pero cuando él/ella abre el archivo PDF, me sale el siguiente mensaje de error:
"Acrobat podría no oen 'nombre_archivo', ya que es o bien no es un tipo de archivo compatible o porque el archivo se ha dañado (por ejemplo, se envió como un archivo adjunto de correo electrónico y no se decodificó correctamente) ... "
Tenga en cuenta que esta es mi primera vez haciendo un proyecto para crear un archivo PDF adjunto. Si alguien pudiera ayudarme a resolver este problema, sería genial. ¡Gracias!
Aquí está mi código:
<?php
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffinfo";
mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)
//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Hello World!");
// email information
$to = $_POST['email'];
$from = $staffEmail;
$subject = "Thank you for your business";
$message = "Thank you for submitting your information!";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "yourinformation.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
//$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Type: application/zip; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, $message, $headers);
}
}
?>
me di cuenta de que tiene application/zip. Pruebe la aplicación/pdf en su lugar. – Raisen
Lo hice también, pero no funcionará. –
Aún no se ha pensado bien en ello, pero IIOL el eol para encabezados MIME siempre es "\ r \ n", el valor real en 'PHP_EOL' varía según el SO; eso podría poner a una llave en las obras en tus $ headers var; aunque de acuerdo con la especificación debe ser tolerante: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html (19.3) – CD001