How to embed image in php mail function

To embed an image in a PHP mail function, you can use the embed or base64_encode functions to encode the image data, and then include it in the email body using HTML.

Here's an example of how you can do it:

<?php

// Set the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";

// Set the email body
$body = '<html><body>';
$body.= '<img src="data:image/jpeg;base64,'. base64_encode(file_get_contents('image.jpg')). '">';

// Set the email subject
$subject = 'Test Email with Embedded Image';

// Set the email recipient
$to = '[email protected]';

// Send the email
mail($to, $subject, $body, $headers);

?>

In this example, we're using the base64_encode function to encode the contents of the image.jpg file, and then including it in the email body using the data URI scheme. The data URI scheme is a way to embed binary data, such as images, directly into an HTML document.

You can also use the embed function to embed the image, like this:

<?php

// Set the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";

// Set the email body
$body = '<html><body>';
$body.= '<img src="cid:image.jpg">';

// Set the email subject
$subject = 'Test Email with Embedded Image';

// Set the email recipient
$to = '[email protected]';

// Set the email attachments
$attachments = array('image.jpg' => file_get_contents('image.jpg'));

// Send the email
mail($to, $subject, $body, $headers, $attachments);

?>

In this example, we're using the embed function to embed the image.jpg file as an attachment, and then referencing it in the email body using the cid attribute.

Note that in both examples, you'll need to make sure that the image file is in the same directory as your PHP script, or provide the full path to the image file.

Also, keep in mind that some email clients may not support embedded images, so it's always a good idea to provide a link to the image file instead of embedding it.