0

I am trying to create a file using dompdf and attach it to mime without saving. I got the answer from https://stackoverflow.com/a/12008553/9727103, so I used it but email contains something not expected.

This is my partial code.

$pdf = $dompdf->output();

$mm = new Mail_mime();

$msg = 'Hello world to all.';

$mm->setTxtBody($msg);
$mm->addAttachment($pdf,'application/pdf','output.pdf', false);

$body = $mm->get();
$subject = 'Hello World - ['.$customer.']';

$recipients = 'a@b.com';
$headers['From']= 'ab@bc.com'; 
$headers['To']= 'a@b.com';
$headers['Subject'] = $subject;
$params['host'] = 'smtp.sendgrid.net';
$params['port'] = '587';
$params['auth'] = true;
$params['username'] = 'my@user.name';
$params['password'] = 'my@user.pass';


$mail_object = Mail::factory('smtp', $params); // Print the parameters you are using to the page

// Send the message
$mail_object->send($recipients, $headers, $body);
if (PEAR::isError($mail_object)) {
    echo("<p>" . $mail_object->getMessage() . "</p>");
} else {
    echo("Message sent!");
}

It sends successfully but the email I received is this.

--=_0db58b483b7f7e450132520a9d0c50fe
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

Hello World to all.
--=_0db58b483b7f7e450132520a9d0c50fe
Content-Transfer-Encoding: base64
Content-Type: application/pdf;
 name=output.pdf
Content-Disposition: attachment;
 filename=output.pdf;
 size=5444

JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2Vz
IDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+Pgpl
bmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCl0KL0NvdW50IDEKL1Jl
c291cmNlcyA8PAovUHJvY1NldCA0IDAgUgovRm9udCA8PCAKL0YxIDggMCBSCi9GMiA5IDAgUgo+
PgovWE9iamVjdCA8PCAKL0kxIDE0IDAgUgo+PgovRXh0R1N0YXRlIDw8IAovR1MxIDE1IDAgUgov
R1MyIDE2IDAgUgo+Pgo+PgovTWVkaWFCb3ggWzAuMDAwIDAuMDAwIDYxMi4wMDAgNzkyLjAwMF0K
ID4+CmVuZG9iago0IDAgb2JqClsvUERGIC9UZXh0IC9JbWFnZUMgXQplbmRvYmoKNSAwIG9iago8
PAovUHJvZHVjZXIgKP7/AGQAbwBtAHAAZABmACAAKwAgAEMAUABEAEYpCi9DcmVhdGlvbkRhdGUg
....

Attached file is converted to string and there is

Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

Which I didn't intend. When I use https://www.freeformatter.com/base64-encoder.html#ad-output and copy the pdf string and click 'decode and download' it downloads the pdf file I attached, but I want it to be a just regular email with the attached file.

Is there any way I could resolve this problem? Any help will be appreciated. Thanks!

Saeed
  • 2,169
  • 2
  • 13
  • 29
Min Lee
  • 347
  • 1
  • 3
  • 8

1 Answers1

0

I think you're missing the point. The PDF is correctly being attached to your email. Your email (according to your post) contains:

Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

And:

Content-Transfer-Encoding: base64
Content-Type: application/pdf;
 name=output.pdf
Content-Disposition: attachment;
 filename=output.pdf;
 size=5444

If you are looking at this email in a email client, you will see that the PDF appears as an attachment, and the message in the email will "Hello World to all." in plain text (i.e. no formatting).

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
  • The recipient has been set to my email so I am basically the customer. However, that is the one I received with Content stuffs and without any attachment. Without attaching pdf output it clearly shows plain text but when I attach the pdf it turns like that.. – Min Lee Sep 07 '18 at 18:09
  • OK. It looks like you were posting the raw email, and not the email itself. It looks like your code is sending the raw email rather than the formatted version. Or it could be the email client you're using. What client are you seeing this email in? – Niraj Shah Sep 08 '18 at 13:39