14

How to insert image in mail body when user click on send button. I am using php mail

ZEESHAN IQBAL
  • 273
  • 2
  • 7
  • 21

4 Answers4

32

To create an HTML email you can do something like this:

...
$message = "<html><head></head><body>";
$message .= "<img src='link-image.jpg' alt='' /></body></html>";

$headers = "From: $from_email";
$headers .= "Content-type: text/html";

mail($to, $subject, $message, $headers);

This should build an HTML email for you and you should then be able to insert just normal html.

edit You can read more about how to create HTML emails using PHP from here: http://css-tricks.com/sending-nice-html-email-with-php/

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
sarcastyx
  • 2,219
  • 17
  • 16
  • 3
    I tried this, and it worked however. You need to add the absolute path. Like http://www.example.com/image.jpg And nog just image.jpg – user3806549 Nov 01 '16 at 13:41
7

If you are actually asking: How to attach and insert inline images in a html email? you can use this for guidance :) https://www.quora.com/What-is-meant-by-inline-images-in-HTML

In that example, pay extra attention to how the src attribute of the img tag is filled (the "cid" is actually the id given as "Content-ID:" for the image attachment header).

Hope this helps, all the best...

Catalin
  • 858
  • 5
  • 16
  • I don't see any mention of "cid" or "Content-ID" on the linked page. For reference, see [`Content-ID` @ How to embed images in email](https://stackoverflow.com/a/4312865/924299). – showdev Mar 24 '20 at 20:18
4

To insert image in body of mail, you can use phpmailerclass which links are

http://www.phpclasses.org/package/264-PHP-Full-featured-email-transfer-class-for-PHP.html

http://sourceforge.net/projects/phpmailer/

Sanjeev Kumar Jha
  • 1,213
  • 2
  • 12
  • 20
1

The correct is almost as the above marked answer. One most important part omitted is the absolute part of the image as indicated below:

$message = "<html><head></head><body>";
$message .= "<img src='http://example.com/images/link-image.jpg' alt='' /></body></html>";

$headers = "From: $from_email";
$headers .= "Content-type: text/html";

mail($to, $subject, $message, $headers);
showdev
  • 28,454
  • 37
  • 55
  • 73
Magige Daniel
  • 1,024
  • 1
  • 10
  • 10