PHPmailer and pdf attachment

40,061

Solution 1

Try this, it work for me ...

   $mail->AddAttachment('path_to_pdf', $name = 'Name_for_pdf',  $encoding = 'base64', $type = 'application/pdf');

In your case

    $mail->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/facturen/test.pdf', $name = 'test',  $encoding = 'base64', $type = 'application/pdf');

Solution 2

you can use three more parameters, all of which are optional:

AddAttachment($path,$name,$encoding,$type);

also follow this answer: Send File Attachment from Form Using phpMailer and PHP

There is an additional way to add an attachment. If you want to make a HTML e-mail with images incorporated into the desk, it's necessary to attach the image and then link the <img src="cid:CID" /> tag to it. For example, if you add an image as inline attachment with the CID my-photo, you would access it within the HTML e-mail with <img src="cid:my-photo" alt="my-photo" />.

In detail, here is the function to add an inline attachment:

$mail->AddEmbeddedImage(filename, cid, name);

By using this function with this example's value above, results in this code:

$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
Share:
40,061
FredV
Author by

FredV

Updated on August 01, 2020

Comments

  • FredV
    FredV over 3 years

    I'm trying to send an email with a PDF attachment using the phpmailer class.

    I used this code:

    Within mailTo function:

    $mail->AddAttachment($pdffile);
    

    where $pdffile = $_SERVER['DOCUMENT_ROOT'] . "/facturen/test.pdf"

    Sending the mail results in an email without attachment but with:

    --b1_3768f40f33d9a5dec555d03a15af14f9
    Content-Type: text/html; charset = "iso-8859-1"
    Content-Transfer-Encoding: 8bit
    

    at the top of my email and on the bottom of my email:

    --b1_3768f40f33d9a5dec555d03a15af14f9 
    Content-Type: application/octet-stream; name="test.pdf" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment; filename="test.pdf" 
    
    JVBERi0xLjQKJcfsj6IKMTEgMCBvYmoKPDwvTGVuZ3RoIDEyIDAgUi9GaWx0ZXIgL0ZsYXRlRGVj
    b2RlPj4Kc3RyZWFtCnic7VtZs9zEFa5A2CYpQyALSUiiNyQqI3pfeGOrOECgMJc8BPJg38Xbta8x
    NqbyL/Jn85yvF6mPpNbMXMoPVCrlskvT6j59+izfWdT+pmG9UA0Lf4aH43ubt69x2dz8dsOb8Ofh
    zY2zrGfeNU7yXmvf3Ns4wXrDxDhiHes50431vGdGlt8K741pjjfDiMEMbZuBgNHhp2qGLYbfxxvP 
    fa+9Gke8CTOG9flXoT8MZAaG1QOD8yMcb87e2sieCwXST3DUj/D3Tj7ytb/8bx751uaLzTcNV1r3 
    0kVtcy5Vr3ijmeqNwFzfc+51tgLVfHCx+XwT7SMIRbMgsUAZkzkPlFygBCq2V83D00jfOZAUJtE3 
    eHamwSoBJhvJlOidzfR1or97iXAWm/i8xBy0xHLZDytsfYXoPV2hNes9z0tcWgKxKq5NMA8RVJCn 
    QVzO6UjKaJxb6XTyJCa4yzcb1XNjjI 
    

    and so on...

    How to resolve this?

  • FredV
    FredV over 10 years
    Thank you, but i'm trying to attach a PDF. I know about the $name,$encoding,$type parameters, but how do they help me in this case?