Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi Team

I need some help, i have a logic that attached document as pdf, this document does saves to the database well. But the problem i cant seem to achieve get the document as pdf on the email, only shows text its not right to a user and need some help based into what i have made.

What I have tried:

<pre><?php
   $to = "Ongeziwe.Gogotya@agilelimitless.org.za";
   $from = $_REQUEST['email'];
   $amount =$_REQUEST['amount'];
   $purpose =$_REQUEST['purpose'];
   $voucher =$_REQUEST['voucher'];
   $gender =$_REQUEST['gender'];
   $fname =$_REQUEST['fname'];
   $lname =$_REQUEST['lname'];
   $title =$_REQUEST['title'];
   $typeID =$_REQUEST['typeID'];
   $email =$_REQUEST['email'];
   $number =$_REQUEST['number'];
   $maritalstatus =$_REQUEST['maritalstatus'];
   $street1 =$_REQUEST['street1'];
   $street2 =$_REQUEST['street2'];
   $town =$_REQUEST['town'];
   $province =$_REQUEST['province'];
   $code =$_REQUEST['code'];
   $propertyownership =$_REQUEST['propertyownership'];
   $placeofwork =$_REQUEST['placeofwork'];
   $jobtitle =$_REQUEST['jobtitle'];
   $jobstreet=$_REQUEST['jobstreet'];
   $jobstreet2 =$_REQUEST['jobstreet2'];
   $jobtown =$_REQUEST['jobtown'];
   $worknumber =$_REQUEST['worknumber'];
   $jobprovince =$_REQUEST['jobprovince'];
   $jobcode =$_REQUEST['jobcode'];
   $income =$_REQUEST['income'];
   $bankname=$_REQUEST['bankname'];
   $branchname =$_REQUEST['branchname'];
   $accountno =$_REQUEST['accountno'];
   $accounttype =$_REQUEST['accounttype'];
   $signature =$_REQUEST['signature'];
   $id =$_REQUEST['id'];
   $bankstatement =$_REQUEST['bankstatement'];
   $payslip=$_REQUEST['payslip'];
   
    $headers = "From: $from";
	$headers = "From: " . $from . "\r\n";
	$headers .= "Reply-To: ". $from . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
    $subject = "Application Form";
    

    $logo = 'img/logo.png';
    $link = '#';

	$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
	$body .= "<table style='width: 100%;'>";
	$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
	$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
	$body .= "</td></tr></thead><tbody><tr>";
	$body .= "<td style='border:none;'>Name: {$fname} {$lname}</td>";
	$body .= "<td style='border:none;'>Email: {$from}</td>";
	$body .= "</tr>";
	$body .= "<tr><td style='border:none;'>Upload ID: {$id}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>CellNumber: {$number}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>Loan Amount: {$amount}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>Monthly income: {$income}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>Town: {$town}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>Upload bankstatement  {$bankstatement}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'>Upload payslip: {$payslip}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td colspan='2' style='border:none;'> </td></tr>";
	$body .= "</tbody></table>";
	$body .= "</body></html>";

    $send = mail($to, $subject, $body, $headers);
    
   $amount  =$_POST['amount'];
   $purpose =$_POST['purpose'];
   $voucher =$_POST['voucher'];
   $gender =$_POST['gender'];
   $fname =$_POST['fname'];
   $lname =$_POST['lname'];
   $title =$_POST['title'];
   $typeID =$_POST['typeID'];
   $email =$_POST['email'];
   $number =$_POST['number'];
   $maritalstatus =$_POST['maritalstatus'];
   $street1 =$_POST['street1'];
   $street2 =$_POST['street2'];
   $town =$_POST['town'];
   $province =$_POST['province'];
   $code =$_POST['code'];
   $propertyownership =$_POST['propertyownership'];
   $placeofwork =$_POST['placeofwork'];
   $jobtitle =$_POST['jobtitle'];
   $jobstreet=$_POST['jobstreet'];
   $jobstreet2 =$_POST['jobstreet2'];
   $jobtown =$_POST['jobtown'];
   $worknumber =$_POST['worknumber'];
   $jobprovince =$_POST['jobprovince'];
   $jobcode =$_POST['jobcode'];
   $income =$_POST['income'];
   $bankname=$_POST['bankname'];
   $branchname =$_POST['branchname'];
   $accountno =$_POST['accountno'];
   $accounttype =$_POST['accounttype'];
   $signature =$_POST['signature'];
   $id =$_POST['id'];
   $bankstatement =$_POST['bankstatement'];
   $payslip=$_POST['payslip'];
   
   /*echo ="test one";
   /**
    * @author:Gcobani Mkontwana
    *@date 01/03/2023
    */
   /* attachment as pdf to the email sent to user here.
   */


   if(isset($_POST['button']) && isset($_FILES['attachment']))
{
	$from_email		 = 'Ongeziwe.Gogotya@agilelimitless.org.za'; //from mail, sender email address
	$recipient_email = 'ggcobani@gmail.com'; //recipient email address
	
	//Load POST data from HTML form
	$sender_name = $_POST["sender_name"]; //sender name
	$reply_to_email = $_POST["sender_email"]; //sender email, it will be used in "reply-to" header
	$subject	 = $_POST["subject"]; //subject for the email
	$message	 = $_POST["message"]; //body of the email

	
	//Get uploaded file data using $_FILES array
	$tmp_name = $_FILES['attachment']['tmp_name']; // get the temporary file name of the file on the server
	$name	 = $_FILES['attachment']['name']; // get the name of the file
	$size	 = $_FILES['attachment']['size']; // get size of the file for size validation
	$type	 = $_FILES['attachment']['type']; // get type of the file
	$error	 = $_FILES['attachment']['error']; // get the error (if any)

	//validate form field for attaching the file
	if($error > 0)
	{
		die('Upload error or No files uploaded');
	}

	//read from the uploaded file & base64_encode content
	$handle = fopen($tmp_name, "r"); // set the file handle only for reading the file
	$content = fread($handle, $size); // reading the file
	fclose($handle);				 // close upon completion

	$encoded_content = chunk_split(base64_encode($content));
	$boundary = md5("random"); // define boundary with a md5 hashed value

	//header
	$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
	$headers .= "From:".$from_email."\r\n"; // Sender Email
	$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email address to reach back
	$headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type
	$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
		
	//plain text
	$body = "--$boundary\r\n";
	$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
	$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
	$body .= chunk_split(base64_encode($message));
		
	//attachment
	$body .= "--$boundary\r\n";
	$body .="Content-Type: $type; name=".$name."\r\n";
	$body .="Content-Disposition: attachment; filename=".$name."\r\n";
	$body .="Content-Transfer-Encoding: base64\r\n";
	$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
	$body .= $encoded_content; // Attaching the encoded file with email
	
	$sentMailResult = mail($recipient_email, $subject, $body, $headers);

	if($sentMailResult ){
		echo "<h3>File Sent Successfully.<h3>";
		// unlink($name); // delete the file after attachment sent.
	}
	else{
		die("Sorry but the email could not be sent.
					Please go back and try again!");
	}
 $echo = "second test";
   
 
   
   
   $conn = new mysqli('localhost','acifinan_acifinan','Finance_01','acifinan_acifinanc');
   if ($conn->connect_error)
   {die('Connection Failed:'.$conn-> connect_error); }
   else 
  {
      $stmt= $conn->prepare("insert into application(amount, purpose, voucher, gender, fname, lname, title, typeID, email, number, maritalstatus, street1, street2, town, province, code, propertyownership, placeofwork, jobtitle, jobstreet, jobstreet2, jobtown, worknumber, jobprovince, jobcode, income, bankname, branchname, accountno, accounttype, signature, id, bankstatement, payslip) values (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");
      $stmt-> bind_param("ssssssssssssssssssssssssssssssssss" , $amount,$purpose,$voucher,$gender,$fname,$lname,$title,$typeID,$email,$number,$maritalstatus,$street1,$street2,$town,$province,$code,$propertyownership,$placeofwork,$jobtitle,$jobstreet,$jobstreet2,$jobtown,$worknumber,$jobprovince,$jobcode,$income,$bankname,$branchname,$accountno,$accounttype,$signature,$id,$bankstatement,$payslip);
      $stmt->execute();
      echo "Submitted Successfully";
      $stmt->close();
      $conn->close(); 
      
  }
  
  /*echo ="test one";
   /**
    * @author:Gcobani Mkontwana
    *@date 01/03/2023
    */
   /* attachment as pdf to the email sent to user here.
   */


   if(isset($_POST['button']) && isset($_FILES['attachment']))
{
	$from_email		 = 'Ongeziwe.Gogotya@agilelimitless.org.za'; //from mail, sender email address
	$recipient_email = 'ggcobani@gmail.com'; //recipient email address
	
	//Load POST data from HTML form
	$sender_name = $_POST["sender_name"]; //sender name
	$reply_to_email = $_POST["sender_email"]; //sender email, it will be used in "reply-to" header
	$subject	 = $_POST["subject"]; //subject for the email
	$message	 = $_POST["message"]; //body of the email

	
	//Get uploaded file data using $_FILES array
	$tmp_name = $_FILES['attachment']['tmp_name']; // get the temporary file name of the file on the server
	$name	 = $_FILES['attachment']['name']; // get the name of the file
	$size	 = $_FILES['attachment']['size']; // get size of the file for size validation
	$type	 = $_FILES['attachment']['type']; // get type of the file
	$error	 = $_FILES['attachment']['error']; // get the error (if any)

	//validate form field for attaching the file
	if($error > 0)
	{
		die('Upload error or No files uploaded');
	}

	//read from the uploaded file & base64_encode content
	$handle = fopen($tmp_name, "r"); // set the file handle only for reading the file
	$content = fread($handle, $size); // reading the file
	fclose($handle);				 // close upon completion

	$encoded_content = chunk_split(base64_encode($content));
	$boundary = md5("random"); // define boundary with a md5 hashed value

	//header
	$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
	$headers .= "From:".$from_email."\r\n"; // Sender Email
	$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email address to reach back
	$headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type
	$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
		
	//plain text
	$body = "--$boundary\r\n";
	$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
	$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
	$body .= chunk_split(base64_encode($message));
		
	//attachment
	$body .= "--$boundary\r\n";
	$body .="Content-Type: $type; name=".$name."\r\n";
	$body .="Content-Disposition: attachment; filename=".$name."\r\n";
	$body .="Content-Transfer-Encoding: base64\r\n";
	$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
	$body .= $encoded_content; // Attaching the encoded file with email
	
	$sentMailResult = mail($recipient_email, $subject, $body, $headers);

	if($sentMailResult ){
		echo "<h3>File Sent Successfully.</h3><h3>";
		// unlink($name); // delete the file after attachment sent.
	}
	else{
		die("Sorry but the email could not be sent.
					Please go back and try again!");
	}
 $echo = "third one test";
   
    

?></h3>
Posted
Updated 1-Mar-23 7:03am
v3

1 solution

Are you trying to send the PDF as an attachment in an email? Your question isn't very clear on this.

Also, you're code seems to be "monolithic", meaning doing more than one thing at a time. Break this code down into multiple functions. One to parse the data coming back from the page, another to generate the PDF, and another to generate the email and send it.
 
Share this answer
 
Comments
Gcobani Mkontwana 1-Mar-23 14:35pm    
@Dave i know, how can break this into small segments to allow send pdf as an attachment to the email?
Dave Kreskowiak 1-Mar-23 23:22pm    
A function should do one thing and one thing only. That stuff that's inserting data into a database should be its own function. Building the body of the email should be its own function Adding the attachment to the email should its own function. ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900