-1

I am using the below code and it's working. However the attached code does not work, and sending mail failed. Could it be to do with the wrong header?

Here the $htmlTable variable is used for collecting form post value and coverting it to pdf using the html to pdf function. But the mail is not sent with pdf.

$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
//Create PDF file with data
$semi_rand = md5(time());
$pdf1 = $htmlTable;

 $file = $semi_rand . ".pdf"; 
 $handle = fopen($file, 'w+');
 fwrite($handle, $pdf);   
 fclose($handle);
 // Additional headers
   $subject = "User Form";
    $content = chunk_split(file_get_contents($file));
       $uid = md5(uniqid(time()));  //unique identifier

       //standard mail headers
       $header = "From: ".$from."\r\n";
       $header .= "Reply-To: ".$_POST['email']. "\r\n";
       $header .= "MIME-Version: 1.0\r\n";


       //declare multiple kinds of email (plain text + attch)
       $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
       $header .="This is a multi-part message in MIME format.\r\n";

       //plain txt part

      //$header .= "--".$uid."\r\n";
     // $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
     // $header .= "Content-Transfer-Encoding: bit\r\n\r\n";
      //$header .= 'sample'. "\r\n\r\n";


       //attch part
       $header .= "--".$uid."\r\n";
       $header .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n";
       $header .= "Content-Transfer-Encoding: base64\r\n";
       $header .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
       $header .= $content."\r\n\r\n";  //chucked up 64 encoded attch
        $success = mail($to,$subject,'PFA',$header);
       if (!$success) {
      echo "Mail to " . $to . " failed .";
       }else {
      echo "Success : Mail was send to " . $to ;
      print_r ($header);
       }
sourabh
  • 152
  • 1
  • 9
  • 1
    You tagged this question with [PHPMailer](https://github.com/PHPMailer/PHPMailer), but you're not using it. You should, then you don't need to worry about how to build attachments properly. Generally, solve one problem at a time - check your PDF build is working before trying to send it. Also, look at `file_put_contents`, it's much simpler than faffing with `fopen`. – Synchro Apr 28 '16 at 10:43
  • Yes my pdf build is working but i am getting problem in to sending attachment with mail... – sourabh Apr 28 '16 at 11:20
  • How can the "attached code" be working and the "code below" not working? – symcbean Apr 28 '16 at 11:20
  • 1
    And if it were working it would be vulnerable to email injection. – symcbean Apr 28 '16 at 11:21
  • I am saying that the form data is converted in to PDF but I have not find the solution how to send pdf buffered data to mail – sourabh Apr 28 '16 at 11:35

1 Answers1

2

You should be using PHPMailer class as it's simpler to attach a file and send it by email.

An example of sending an email with attachment can be:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Attachment is here';
$mail->Body    = 'Here goes the attachment;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

I've slightly modified the example which can be found in the link I've shared above.

As Synchro suggested you can use addStringAttachment method, in order to skip the whole writing the PDF to a file step and send it directly from memory.

João
  • 23
  • 6
rafaelcpalmeida
  • 874
  • 1
  • 9
  • 28
  • 1
    If you make use of PHPMailer's `addStringAttachment` method, you can skip the whole writing the PDF to a file step and send it directly from memory. – Synchro Apr 28 '16 at 11:23