skip to Main Content

I’m trying to send an email with a pdf form completed from a HTML form.

I’ve been successful at taking the HTML input values and creating a PDF with those values that is show in the browser via $pdf->Output(); using fpdm but I cannot get it to be sent as an attachment. Right now I receive an email with a .PDF file attached that cannot be opened.

The code is as follows:

require_once 'fpdm-master/fpdm.php';

if (isset($_POST['rentstatus']) && $_POST['rentstatus'] == 'Yes') {
    $rentstatus = "Yes";
} else {
    $rentstatus = "No";
}

/***************************
  Sample using a PHP array
****************************/
$name = $_POST['name'];
$email = $_POST['email'];
$useraddress = $_POST['address'];
$usercity = $_POST['city'];
$userstate = $_POST['state'];
$usercode = $_POST['code'];
$usercell = $_POST['cell'];
$userarival = $_POST['arival'];
$userdeparture = $_POST['departure'];

$fields = array(
    'name'=> $name,
    'phone'=> $usercell,
    'date'=> $userdeparture,
    'email'=> $email,
    'signature'=> $name,
    'datenow' => date("M d, Y"),
    'yes' => true,
    'no' => false
);

$pdf = new FPDM('template.pdf');
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
// encode data (puts attachment in proper format)
// attachment name
$filename = "test.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc)); 

$message = "rn Strata Lot: 28 of Strata Plan: EPS 7373 rn Name(s) of tenant(s): " . $name . "rn Telephone: " . $usercell . "rn Email: " . $email . "rn Tenancy commencing: " . $userdeparture . "rn Is this unit rented to a family member? - " . $rentstatus .  "rn Date: " . $datenow . "rn Address, Phone Number and Email Address of Landlord or Agent of Landlord: 103-1550 Duchess Ave, West Vancouver, B.C. V7V 1P5 rn Name of Landlord or Agent of Landlord: Donald Kim rn rn rn For more information, reply to this email and we will get back to you shortly. rn - Restavio Team";

$subject = "Form K for " . $name;
$fromname = " ";
$fromemail = " "; //if u dont have an email create one on your cpanel
$mailto = ' '; //the email which u want to recv this email
$secondmail = ' '; //the email which u want to recv this email

// carriage return type (RFC)
$eol = "rn";
// main header (multipart mandatory)
$headers = "From: " . $fromname . " <" . $fromemail . ">" . $eol;
$headers .= 'Cc: [email protected]' . "rn";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary="" . $separator . """ . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset="iso-8859-1"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name="".$filename.""".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
    echo "<p style='font-size:2rem; text-align:center'>Thank you. Your information was sent. You will be redirected shortly.</p>";
    header("refresh:2;url=https://restavio.com/1111_richards_st_vc/index.html");
} else {
    echo "mail send ... ERROR!";
    print_r(error_get_last());
}

As a TL;DR -> PDF file breaks when trying to use it as attachment in email

2

Answers


  1. Chosen as BEST ANSWER

    I was able to make this work by saving the pdf locally and attaching it afterwards.

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", $filename);
    $content = file_get_contents($filename);
    $content = chunk_split(base64_encode($content));
    

    Surely my original code had issues with passing the string along, probably I did not know how to pass the content along. This is but a easy fix for my needs, there surely is a way more elegant way of doing this using something like PHPMailer like CBroe said.


  2. And if you use this code…?

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("S");
    $attachment = chunk_split(base64_encode($pdfdoc));
    

    You have a problem with your use of the "output" function, don’t you…?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search