Home  >  Article  >  Backend Development  >  How Do I Send HTML Emails with Embedded Images Using Libraries?

How Do I Send HTML Emails with Embedded Images Using Libraries?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 21:17:30211browse

How Do I Send HTML Emails with Embedded Images Using Libraries?

Embedding Images in HTML Emails

Sending HTML emails with embedded images can be accomplished with the help of libraries like PHPMailer. This library streamlines the process and handles most issues automatically.

Displaying Embedded Images

Embedded images are incorporated into the email's HTML code. To access them, use the following syntax:

<img src="cid:my-photo" alt="my-photo" />

where "my-photo" is the CID (Content-ID) of the attachment.

Adding Embedded Images with PHPMailer

In PHPMailer, you can embed images using the following function:

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

For instance, to embed "my-photo.jpg" with the CID "my-attach":

$mail->AddEmbeddedImage("my-photo.jpg", "my-attach", "my-photo.jpg ");

Example HTML Email with Embedded Image

<code class="html"><!DOCTYPE html>
<html>
  <head>
    <title>Embedded Image</title>
  </head>
  <body>
    <p>Here is an image embedded in HTML: <img src="cid:my-attach"></p>
  </body>
</html></code>

Sending the Email

<code class="php">// Using PHPMailer to build the message
$mail->Send();</code>

Alternatively, you can retrieve the message content using the following code and send it using your preferred method:

<code class="php">$mime_message = $mail->CreateBody(); //Retrieve the message content
echo $mime_message; // Echo it to the screen or send it using whatever method you want</code>

The above is the detailed content of How Do I Send HTML Emails with Embedded Images Using Libraries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn