Jump to content

fpdf to email then also send uploaded files php


Dan152

Recommended Posts

i am trying to create a small script that will let you

 

1. Create a PDF from form fields then email that pdf without saving it (was using fpdf for this)

2. within that form there is a file upload - i want to send these along with the pdf crerated in the same email.

 

this is my code so far, the php sends fine but it is sending in 2 seperate emails and i want it all to send in one

 

any help would be much apprichated as i am a compleat noob at PHP!

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Attachment Without Upload - Excellent Web World</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;}
th{ background:#999999; text-align:right; vertical-align:top;}
input{ width:181px;}
</style>
</head>
<body>
    <form action="emailSend.php" method="post" name="mainform" enctype="multipart/form-data"> 
    <table width="500" border="0" cellpadding="5" cellspacing="5">
       <tr>
        <th>Your Name</th>
        <td><input name="fieldFormName" type="text"></td>
    </tr>
    <tr>
    <tr>
        <th>Your Email</th>
        <td><input name="fieldFormEmail" type="text"></td>
    </tr>
    <tr>
        <th>To Email</th>
        <td><input name="toEmail" type="text"></td>
    </tr>
 
    <tr>
        <th>Subject</th>
        <td><input name="fieldSubject" type="text" id="fieldSubject"></td>
    </tr>
    <tr>
        <th>Comments</th>
        <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td>
    </tr>
    <tr>
      <th>Attach Your File</th>
      <td><input name="attachment" type="file"></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
    </tr>
    </table>
    </form>
</body>
</html>
<?php
 
//create PDF and send
// download fpdf class (http://fpdf.org)
require("fpdf.php");
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
// email stuff (change data below)
$to = "me@domain.com";
$from = "me@domain.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// 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 = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; 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, "", $headers);



//send uploaded attachments via email
 
$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$subject = $_POST['fieldSubject'];
$message = $_POST['fieldDescription'];
 
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
 
/* Start of headers */
$headers = "From: $fromName";
 
if (file($tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb');
  $data = fread($file,filesize($tmpName));
  fclose($file);
 
  /* a boundary string */
  $randomVal = md5(time());
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
 
  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";
 
  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mimeBoundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $message . "\n\n";
 
  /* Encoding file data */
  $data = chunk_split(base64_encode($data));
 
  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" .
  "Content-Type: {$fileType};\n" .
  " name=\"{$fileName}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n" .
  "--{$mimeBoundary}--\n";
}
 
$flgchk = mail ("$to", "$subject", "$message", "$headers");
 
if($flgchk){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.