0

I cannot seem to get attachments to send through PHPMailer. Here is what I have for the code to send the email :

REVISED Automailer Content:

if(isset($_POST['type']) && $_POST['type'] == 'sendEmail' ){
    require_once 'phpMail/PHPMailerAutoload.php';
    $email = $_POST['email'];
    $uuid = $_POST['uuid'];
    $file_content = file_get_contents(BASE_PATH.'email.php?uuid='.$uuid);
    $pdf_content = file_get_contents(BASE_PATH.'pdf.php?uuid='.$uuid);
    $mail = new PHPMailer;
    $mail->From = ADMIN_EMAIL;
    $mail->FromName = SENDER_NAME;
    $mail->addAddress( $email );               // Name is optional
    $mail->addReplyTo(REPLY_EMAIL, 'Reply Mail');
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Email From '.User;
    $mail->Body    = $file_content;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->AddStringAttachment($output, attach.pdf);
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo "true";
    }

    exit;
}

REVISED Dompdf Code(pdf.php)

    $html .= //Content Here
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
  if ($PAGE_NUM > 1) {
    $font = Font_Metrics::get_font("helvetica", "bold");
    $current_page = $PAGE_NUM-1;
    $total_pages = $PAGE_COUNT-1;
    $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
  }
');
$output = $dompdf->output();
file_put_contents('Attachment.pdf', $output);

If I remove the code concerning the attachment, the email sends just fine(with email.php in the body). Attachments, however refuse to work...

Steven
  • 687
  • 1
  • 10
  • 27
  • Well did you save the file on your server before sending it with phpmailer? – Naruto Mar 29 '16 at 14:30
  • @Naruto First of all, cool name ;D. Second, I did not want to save the file to the server if I did not have to, If I have to, I want it to only to save to a temporary file, Third, I am pretty fresh to PHPAutoMailer, and am not sure of how to do that :D Best comment would be that I would go with whatever is the best practice, even if it meant I had to save to server. – Steven Mar 29 '16 at 14:33
  • `$dompdf->stream()` method is used to open a download dialog on the user browser. So you can't use it for an attachment. Use Naruto method below. – ThinkTank Mar 29 '16 at 14:34
  • Well just save it to the server, mail it, and then remove it from the server? I think (not 100% sure), that the file needs to exists before you can mail it. And thx :) – Naruto Mar 29 '16 at 14:35
  • @Naruto Pretend that I am a gorilla(or something). Is there a guide/tutorial/anything online that will show me how to save it to the server, and then retrieve it using phpmailer? – Steven Mar 29 '16 at 14:42
  • yeah.. It's called google... http://stackoverflow.com/questions/8720897/how-to-save-dompdf-generated-content-to-file – Naruto Mar 29 '16 at 14:44
  • @Naruto Ok, that looks simple enough, however, the file that I will be saving will be dynamically generated each and every time.... How would I point to that file in that scenario? – Steven Mar 29 '16 at 14:46

1 Answers1

0

Working Code :

Server Code :

if(isset($_POST['type']) && $_POST['type'] == 'sendEmail' ){
    require_once 'phpMail/PHPMailerAutoload.php';
    $email = $_POST['email'];
    $uuid = $_POST['uuid'];
    $file_content = file_get_contents(BASE_PATH.'email.php?uuid='.$uuid);
    $pdf_content = file_get_contents(BASE_PATH.'pdfemail.php?uuid='.$uuid);
    $mail = new PHPMailer;
    $mail->From = ADMIN_EMAIL;
    $mail->FromName = SENDER_NAME;
    $mail->addAddress( $email );               // Name is optional
    $mail->addReplyTo(REPLY_EMAIL, 'Reply Mail');
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Project From '.COMPANY_NAME;
    $mail->Body    = $file_content;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->AddAttachment('./attachments/attach.pdf', "Attachment.pdf");
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo "true";
    }

    exit;
}

Dompdf File :

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
  if ($PAGE_NUM > 1) {
    $font = Font_Metrics::get_font("helvetica", "bold");
    $current_page = $PAGE_NUM-1;
    $total_pages = $PAGE_COUNT-1;
    $pdf->text(522, 770, "Page: $current_page of $total_pages", $font, 10, array(0,0,0));
  }
');
$output = $dompdf->output();
$file_to_save = './attachments/attach.pdf';
file_put_contents($file_to_save, $output);
Steven
  • 687
  • 1
  • 10
  • 27