1

I am trying to attach an email directly to another another email.

Here is the source

        $mailer = new PHPMailer();
        $message = '<p>Some message body text<>';
        $mailer->Subject = 'Email subject';
        $mailer->addAddress('[email protected]');
        $mailer->Body = $message;
        $mailer->FromName = $_SERVER['SERVER_NAME'];
        $mailer->Sender = $mailer->From = '[email protected]';
        $mailer->isHTML(true);

        // Also attach the raw email as .eml file for better handling/training purposes
        $attachmentMail = new PHPMailer();
        $attachmentMail->FromName = $violationFrom;
        $attachmentMail->addAddress('[email protected]');
        $attachmentMail->Subject = $violationSubject;
        $attachmentMail->isHTML(true);
        $attachmentMail->Body    = $violationBody;

        // Prepare and get raw MIME source
        if (!$attachmentMail->preSend()) {
            echo "preSend failed: " . $attachmentMail->ErrorInfo; die();
        }        
        $rawMime = $attachmentMail->getSentMIMEMessage();

        // Attach the raw MIME as an .eml file
        $mailer->addStringAttachment($rawMime, 'raw-email.eml',  PHPMailer::ENCODING_BASE64, 'message/rfc822');

        // Send the alert email
        $mailer->send();

I get no error when sending the email. In the email I receive there is an attachment called raw-email.eml which also has the proper size. However, no matter the email client I use I can neither download the attachment, nor open it. Any hint why?

2
  • I can't reproduce this. When I run your code, the message is attached correctly and opens just fine. I tried changing the encoding to quoted-printable (which makes the format more visible), but that worked fine too. Commented Nov 13 at 15:00
  • Have you tried resend? I successfully sent a 5MB PDF using resend. Commented Nov 13 at 21:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.