I have a form which a user fills out and submits. This will take the user to a review page where they can then either go back and make changes or submit their information. The user is then taken to a confirmation page.

I used FPDF to create the pdf. I need the information the user filled out to populate this PDF and then email an attachment of the file to their email and to an email with my company. I assume that creating an isset($_POST['Submit'] if statement on the review page and sending the email there would be my best course? I'm really not sure.

I'm new to PHP programming and still trying to learn but I am stuck. I just don't know how to do this. There is A LOT of code involved in this. I'll post the top portions of each page.

Review:

<?php
require_once('../../../../wp-load.php');
require_once('../includes/phpmailer/class.phpmailer.php');
require_once('../classes/504-send-request.php');

if(isset($_POST["Submit"])) {
    //generate pdf

    //save pdf

    //attach pdf to email

    //if commerical mail to mobile homes mhpublicassistance@mail.maricopa.gov   
    $mail = new PHPMailer();
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'mailbox.maricopa.gov';  // Specify main and backup server
    $mail->SMTPAuth = false;                               // Enable SMTP authentication

    $mail->From = 'dillonm@mail.maricopa.gov';
    $mail->FromName = 'Maricopa County Assessor';
    $mail->AddAddress(EMAIL_ADDRESS_504_FORM);  // Add a recipient
    $mail->AddBCC('dillonm@mail.maricopa.gov');
    $mail->IsHTML(true);
    $mail->AddAttachment();  
    $mail->Subject =  "504 Request";
    $mail->Body = 'Attached is a PDF copy of your request. A copy has also been sent to Maricopa County Assessor. Please allow 72 hours (3 business days) for the Assessor\'s Office to process your request. You will be contacted by phone or by email if we have questions regarding the information provided in your request. Once approved, the Tax Clearance will be emailed to you at the email address you\'ve provided. If you have any questions, please contact the Assessor\'s Office at 602-506-3291 or reply to this email.';
    if(!$mail->Send()) {
        print 'No Mail!!! ' . $mail->ErrorInfo;
    }

    //otherwise, mail to pubasst@mail.maricopa.gov
}


$mh = new MobileHomeRequest();
$mh->UpdateFromPost();
$id = $mh->SaveTempData();

Conf:

<?php
require_once('../../../../wp-load.php');
require_once('../includes/phpmailer/class.phpmailer.php');
require_once '../classes/504-send-request.php';
require_once ('../classes/pdf_generator.php');


$mh = new MobileHomeRequest();
$data = $mh->GetTempData($_POST['id']);
//print_r($data);
$data->SaveToDatabase();

pdf-generator.php

<?php

require_once('soap.php');
require_once('504-send-request.php');
require_once('/fpdf/fpdf.php');

//$mh = new MobileHomeRequest();
//$data = $mh->GetTempData($_POST['1']);
//$data = UpdateFromPost();


$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetMargins(10, 10);
$pdf->SetAutoPageBreak(1, 10);

$pdf->SetFont('Arial', 'B', '20');
$pdf->Cell(0, 10, 'Mobile Home Poperty Tax Clearance Request', 0, 1);
$pdf->Ln();

$pdf->SetFont('Arial', 'B', '12');
$pdf->Cell(0, 10, 'This is a REQUEST. This is NOT a property tax clearance.', 0, 1);

$pdf->SetFont('Arial', '', '12');
//$pdf->MultiCell(0, 5, 'NOTE: PURSUANT TO ARS 42-19107 AND 42-19155, it is unlawful for any person to knowingly move or sell a mobile home for which applicable property taxes have not been paid. A person violating the provisions of the section is guilty of a class 1 Misdemeanor and is subject to a fine.', 0, 1);
$pdf->Ln();

$pdf->SetFont('Arial', '', 12);
$pdf->Cell(40, 5, 'Date Issued:');
$pdf->Cell(25, 5, date('m-d-Y'));
$pdf->SetTextColor(255,0,0);
$pdf->Cell(0, 5, '(VOID AFTER 30 DAYS)', 0, 1);

Dani AI

Generated

A compact, reliable flow is: save the final form data to the DB, generate and save the PDF from that final record, attach and send the file with a mail library, then remove (or archive) the temporary PDF. Generating the PDF after calling SaveToDatabase() guarantees the file contains the final values and avoids losing form state. Use a unique temp filename (system temp dir or a writable folder under the app) and delete it after a successful send.

Example (minimal, adapted for clarity):

// create a unique filename in the system temp dir
$filepath = sys_get_temp_dir() . '/mhreq_' . uniqid('', true) . '.pdf';

require_once 'fpdf/fpdf.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',12');
$pdf->Cell(0,6, 'Owner: ' . $data->owner_name, 0, 1);
// ... add other fields from $data ...
$pdf->Output($filepath, 'F'); // save to disk

require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP(); // or configure to use local sendmail
$mail->Host = 'smtp.example.com';
$mail->setFrom('noreply@example.com','Assessor');
$mail->addAddress($data->email);           // applicant
$mail->addAddress('office@example.com');   // company copy
$mail->addAttachment($filepath, basename($filepath));
$mail->Subject = '504 Request';
$mail->Body    = 'Attached is a PDF copy of your request.';
$mail->send();

@unlink($filepath); // cleanup

Troubleshooting and gotchas:

  • Confirm SaveTempData() returns an id and that the review form carries it as a hidden field so GetTempData($id) can load the exact record.
  • Make sure the target folder is writable (permission issues are the most common failure).
  • Verify the PDF file exists before calling addAttachment() and log ErrorInfo from PHPMailer on failure.
  • Avoid sending any output (echo/whitespace) before generating/sending mail when using native mail headers or output methods.
  • For production, prefer PHPMailer (handles MIME, encoding) over hand-built multipart mail; ’s raw MIME example shows the low-level approach, but PHPMailer is safer and simpler.

In the code posted by the main missing pieces are (1) generating/saving the PDF to a real pathname and (2) passing that pathname into the mailer. Ensure the PDF generator either accepts the $data object or calls GetTempData($id) itself so fields populate correctly, then attach the generated file and remove it after sending.

Recommended Answers

All 2 Replies

try this one it will work fine...

<?php
$fileatt = "Profile.pdf"; // Path to the file                  
$fileatt_type = "application/pdf"; // File Type  
$fileatt_name = "Profile.pdf"; // Filename that will be used for the file as the attachment  
$email_from = "test@example.com"; // Who the email is from

$subject = "Company Profile";
$message = "Mail ID request Profile\n\n";
$message .= "User with $email_to has Requested and got the profile pdf \r\n";   
$email_subject = "Profile"; // The Subject of the email  
$email_message = "Thank you for using example.in Please find profile attachment file..\n\n<br>";
//$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  
$email_to = $_POST['email']; // 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-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);  
?>

Thank you for that! I haven't even gotten to the mail part yet :[ But I will try that when I do. :)

I'm still trying to tie in the pdf part. I can't seem to get the fields populated with the data. Then it would need to be saved with a randomly generated name on the server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.