web-dev-qa-db-fra.com

php envoyer un e-mail avec PDF pièce jointe

Je crée un pdf en utilisant FPDF . Le PDF se génère parfaitement et le PDF est également disponible par e-mail. Mais je veux aussi envoyer un message corporel. J'ai essayé avec un message corporel. Exemple de message texte fin This is text message from shohag Mais seule la pièce jointe pdf est disponible et le corps est vide. Voici mon code.

function send_pdf_to_user(){
    if($_REQUEST['action'] == 'pdf_invoice' ){
        require('html2pdf.php');
        $pdf=new PDF_HTML();
        $pdf->SetFont('Arial','',11);
        $pdf->AddPage();

        $text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
        if(ini_get('magic_quotes_gpc')=='1')
        $text=stripslashes($text);
        $pdf->WriteHTML($text);

        //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
        $attach_pdf_multipart = chunk_split( base64_encode( $pdf->Output( '', 'S' ) ) );


        //define the receiver of the email 
        $to = '[email protected]';

        //define the subject of the email 
        $subject = 'Test Invoice'; 
        //create a boundary string. It must be unique 
        //so we use the MD5 algorithm to generate a random hash 
        $random_hash = md5(date('r', time())); 
        //define the headers we want passed. Note that they are separated with \r\n 
        $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
        //add boundary string and mime type specification 
        $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";       



        $msg .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n";
        $msg .= "Content-Transfer-Encoding: base64\r\n";
        $msg .= "Content-Disposition: attachment\r\n";
        $msg .= $attach_pdf_multipart . "\r\n";

        $msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
        $msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $msg .= "<p>This is text message from shohag</p>\r\n\r\n";  

        global $message;
        $message = '';
        $mail_sent = @mail( $to, $subject, $msg, $headers );
        //@mail( $to1, $subject, $msg, $headers );
        if(!empty($mail_sent)):
            $message = "Invoice sent succuessfully";
        else:
            $message = "Error occured. Please try again.";
        endif;
    }
}

Veuillez vérifier mon code et faites-moi savoir plus de possibilité. Merci d'avance.

13
Monirul Islam

Vous pouvez utiliser PHPMailer avec FPDF . Il fonctionne correctement sans tracas. Vous devez modifier le paramètre pour $pdf->Output. Téléchargez et copiez class.phpmailer.php Et PHPMailerAutoload.php Dans votre dossier de travail. Attachez class.phpmailer.php En dessous ou au-dessus de require('html2pdf.php');. Je l'ai déjà fait auparavant, donc cela fonctionnera. Selon votre code, cela devrait fonctionner.

function send_pdf_to_user(){
    if($_REQUEST['action'] == 'pdf_invoice' ){
        require('html2pdf.php');
        require_once('class.phpmailer.php');
        $pdf=new PDF_HTML();
        $pdf->SetFont('Arial','',11);
        $pdf->AddPage();

        $text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
        if(ini_get('magic_quotes_gpc')=='1')
        $text=stripslashes($text);
        $pdf->WriteHTML($text);

        $mail = new PHPMailer(); // defaults to using php "mail()"
        $body = "This is test mail by monirul";

        $mail->AddReplyTo("[email protected]","Test Lernt");
        $mail->SetFrom('[email protected]', 'Test Lernt');

        $address = "[email protected]";
        $mail->AddAddress($address, "Abdul Kuddos");       
        $mail->Subject    = "Test Invoice";       
        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

        $mail->MsgHTML($body);
        //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm       
        $pdf->Output("Test Invoice.pdf","F");
        $path = "Walter Lernt Invoice.pdf";

        $mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
        global $message;
        if(!$mail->Send()) {
          $message =  "Invoice could not be send. Mailer Error: " . $mail->ErrorInfo;
        } else {
          $message = "Invoice sent!";
        }

    }
}
10
Hasib Hasan Arnab

Utilisez ce code simple pour envoyer un e-mail avec une pièce jointe pdf. J'espère que cela vous aidera. Merci.

// Settings
$name        = "Name goes here";
$email       = "[email protected]";
$to          = "$name <$email>";
$from        = "Gyan-Shah ";
$subject     = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$fileatt     = "./test.pdf"; //file location
$fileatttype = "application/pdf";
$fileattname = "newname.pdf"; //name that you want to use to send or you can use the same name
$headers = "From: $from";

// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);

// This attaches the 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}\"";
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mime_boundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $mainMessage  . "\n\n";

$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
  "Content-Type: {$fileatttype};\n" .
  " name=\"{$fileattname}\"\n" .
  "Content-Disposition: attachment;\n" .
  " filename=\"{$fileattname}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
 "--{$mime_boundary}--\n";

// Send the email
if(mail($to, $subject, $message, $headers)) {

  echo "The email was sent.";

}
else {

  echo "There was an error sending the mail.";
}
6
Gyan

Aucune bibliothèque externe n'est vraiment nécessaire. Suivez ce format:

$to          = "[email protected], [email protected]"; // addresses to email pdf to
$from        = "[email protected]"; // address message is sent from
$subject     = "Your PDF email subject"; // email subject
$body        = "<p>The PDF is attached.</p>"; // email body
$pdfLocation = "./your-pdf.pdf"; // file location
$pdfName     = "pdf-file.pdf"; // pdf file name recipient will get
$filetype    = "application/pdf"; // type

// create headers and mime boundry
$eol = PHP_EOL;
$semi_Rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_Rand}x";
$headers       = "From: $from$eol" .
  "MIME-Version: 1.0$eol" .
  "Content-Type: multipart/mixed;$eol" .
  " boundary=\"$mime_boundary\"";

// add html message body
  $message = "--$mime_boundary$eol" .
  "Content-Type: text/html; charset=\"iso-8859-1\"$eol" .
  "Content-Transfer-Encoding: 7bit$eol$eol" .
  $body . $eol;

// fetch pdf
$file = fopen($pdfLocation, 'rb');
$data = fread($file, filesize($pdfLocation));
fclose($file);
$pdf = chunk_split(base64_encode($data));

// attach pdf to email
$message .= "--$mime_boundary$eol" .
  "Content-Type: $filetype;$eol" .
  " name=\"$pdfName\"$eol" .
  "Content-Disposition: attachment;$eol" .
  " filename=\"$pdfName\"$eol" .
  "Content-Transfer-Encoding: base64$eol$eol" .
  $pdf . $eol .
  "--$mime_boundary--";

// Send the email
if(mail($to, $subject, $message, $headers)) {
  echo "The email was sent.";
}
else {
  echo "There was an error sending the mail.";
}
1
omikes