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

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

Patricia Arquette
Release: 2024-10-22 21:17:30
Original
286 people have browsed it

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" />
Copy after login

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);
Copy after login

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

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

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>
Copy after login

Sending the Email

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

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template