Search code examples
phphtmlphpmailer

How to send html email with images background without attaching via PHPMailer?


I am using phpmailer for sending emails, and I want to make a custom logo to be in top for my company ,the problem is the logo appear as attachment , Sow i want to know how to embed images in emails without attaching them? Thank you

$mail->Subject = 'Here is the subbject';
$mail->AddEmbeddedImage('logo.png', 'logo');
$mail->Body = '<html><body>';
$mail->Body = '<img src="cid:logo" style="width:100%"></img>';
$mail->Body = '<h3>Bonjour</h3>';
$mail->Body .='</body></html>';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Solution

  • The body of the email must contain a direct link (not a relative link) to an image and that image should be a jpg. eg:

    <?php
    $body .= <<<EOD
    <a href='https://my-website.com'><img src='https://my-website.com/image.jpg' alt='logo'></img></a>
    EOD;
    ?>