mwo
mwo

Reputation: 85

embedding base64 images in email body

I'm trying to embed base64 images with png icons in email body, but either I get empty images or I get raw code instead of html. I played around with different content-types based on other posts in Stackoverflow I think it should be first multipart/alternative and then multipart/mixed, however I'm not sure what part should go into email's header and what goes to email body. I couldn't get any good tutorial on that. Perhaps somebody knows some tool that converts regular html document into one with embeded images?

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: multipart/alternative; boundary='boundary1'" . "\r\n";
    $headers .= "From: StudentOpen <[email protected]>" . "\r\n";
    $headers .= "Reply-To: [email protected]" . "\r\n";

    $subject = $GLOBALS['tpl']['win_mail_subject'];

    $emailText = '';

    $emailText .= "Content-type:multipart/mixed; boundary='boundary1'" . "\r\n";
    $emailText .= "Content-type:multipart/alternative; boundary='boundary2'" . "\r\n";

    $emailText .= '--boundry1' . "\r\n";
    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
    $emailText .= "Content-Transfer-Encoding: 7bit" . "\r\n";

    $emailText .= "<html>";
    $emailText .= "<head>";
    $emailText .= '<meta http-equiv="content-type" content="text/html; charset=UTF-8">';
    $emailText .= "</head>";
    $emailText .= "<body>";

    $emailText .= $GLOBALS['tpl']['howdy'].' '.$tmp_member_username.',';
    $emailText .= '<br/>';
    $emailText .= $GLOBALS['tpl']['win_mail_description1'];        

    $emailText .= '<img src="cid:facebook.png" alt="Facebook"/>';

    $emailText .= '</body>';
    $emailText .= '</html>';

    // facebook.png

    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "Content-Type: image/png;" . "\r\n";
    $emailText .= "name='facebook.png'" . "\r\n";
    $emailText .= "Content-Transfer-Encoding: base64" . "\r\n";
    $emailText .= "Content-ID: <facebook.png>" . "\r\n";
    $emailText .= "Content-Disposition: inline;" . "\r\n";
    $emailText .= "filename='facebook.png'" . "\r\n";

    $emailText .= "iVBORw0KGgoAAAA...AAElFTkSuQmCC" . "\r\n"; // base64 string

    $emailText .= "--boundry2" . "\r\n";
    $emailText .= "--boundry1" . "\r\n";

    $body = $emailText;

    mail($user_email, $subject, $body, $headers);

What do I do wrong? The message I get as an email is messed up. Starts with --boundry1 and then I get raw text.

Upvotes: 2

Views: 1894

Answers (1)

Sven
Sven

Reputation: 70853

You shouldn't attempt to create a valid MIME email message yourself. While it is possible to do so, it's way easier using a dedicated library which has all the edge cases and pitfalls correctly implemented, and all you need to do is call a couple of methods to send an email. This is also easier to maintain if there is something to change.

The two libraries mentioned in this case are PHPMailer and Swiftmailer. Use either one of them, with code like this:

(adapted from the Swiftmailer example code)

require_once 'lib/swift_required.php';

$message = Swift_Message::newInstance();

// Give the message a subject
$message->setSubject($GLOBALS['tpl']['win_mail_subject']);

// Set the From address with an associative array
$message->setFrom(array('[email protected]' => 'StudentOpen'));

// Set the To addresses with an associative array
$message->setTo(array($user_email));

$emailText = '<html>...omitted here ... 
    <img src="' . $message->embed(Swift_Image::fromPath('facebook.png')) . '" alt="Facebook" />
</html>';

// Give it a body
$message->setBody($emailText, 'text/html');

After that, you send it. Done.

Upvotes: 1

Related Questions