Send Inline Image in Email

Send inline image in email

Try this

 string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:filename\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);

LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);

MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);

Attachment att = new Attachment(filePath);
att.ContentDisposition.Inline = true;

mail.From = from_email;
mail.To.Add(data.email);
mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
mail.Body = String.Format(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
@"<img src=""cid:{0}"" />", att.ContentId);

mail.IsBodyHtml = true;
mail.Attachments.Add(att);

Send inline images using send-mailmessage

You can't do that with Send-MailMessage cmdlet. To include inline image in message you have to write your own function using .NET's Net.Mail.SmtpClient class.

Alternatively, you can just grab David Wyatt's Send-MailMessage function, which already has all you need:

The Send-MailMessage cmdlet exposes most of the basic functionality
for sending email, but you don't get a lot of control over things like
attachments or alternate views. Someone on the TechNet forums was
asking how to embed images into an HTML mail message, and I decided to
write a version of Send-MailMessage that supports this. I started
with a proxy function for the Send-MailMessage cmdlet, so all of the
parameters and usage should be intact. The main difference is that I
added an -InlineAttachments argument, which accepts a hashtable of
pairs in the format 'ContentId'='FilePath'. You can then embed the
resources into an HTML body by using URLs of the format
cid:ContentId.


Sending email with inline image

Finally I come to know how to do this. Here is the source link from where I come to know


$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: youremail@host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

<html>

<head>
<style> p {color:green} </style>
</head>
<body>

A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>

</html>'."\n\n".
$bound;

$file = file_get_contents("http://localhost/a/img/1.jpg");

$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;

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

?>

embedding image in html email

Try to insert it directly, this way you can insert multiple images at various locations in the email.

<img src="data:image/jpg;base64,{{base64-data-string here}}" />

And to make this post usefully for others to:
If you don't have a base64-data string, create one easily at:
http://www.motobit.com/util/base64-decoder-encoder.asp from a image file.

Email source code looks something like this, but i really cant tell you what that boundary thing is for:

 To: email@email.de
Subject: ...
Content-Type: multipart/related;
boundary="------------090303020209010600070908"

This is a multi-part message in MIME format.
--------------090303020209010600070908
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="cid:part1.06090408.01060107" alt="Sample Image">
</body>
</html>

--------------090303020209010600070908
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline;
filename="moz-screenshot.png"

[base64 image data here]

--------------090303020209010600070908--

//EDIT: Oh, i just realize if you insert the first code snippet from my post to write an email with thunderbird, thunderbird automatically changes the html code to look pretty much the same as the second code in my post.

phpmailer sent inline images as attachment

If you attach images to an email, they are attachments, no matter what. If you use them by referring to their cid values from within your HTML, they will render inline as well, but they will still be shown as attachments, because they are. If you refer to images using remote URLs, they will render inline as well (providing you allow your mail client to do so), and they will not show as attachments - that is what the other messages you're seeing will be doing. Generally speaking, referring to images remotely is preferable to embedding/attaching.

How to include inline images in email using MailApp

The documention for sendEmail(message) shows how to add two logos to an email as inline images.

Here's an adaptation from Google's example that will prepend your text blocks with their locale flags. It uses images from Wikimedia, which are freely licensed under Creative Commons.

screenshot

The keys to how this works are:

  • HTML <img> tags in the email body use src='cid:[name]' to reference attached images. These are Content-ID Universal Resource Locators, which reference parts of multipart email messages.
  • You can use inline style on the <img> tags to tell the mail client how to display them. For example, we've scaled the attached images. (Note: it would be less wasteful to use custom images that are the exact size needed.)
  • When the email is encoded for sending, each attached image will be in its own part, labelled with the cid you've provided in the inlineImages object. This object is part of the advanced parameters object for the sendMail() method.

    Here, we have two cids, nlFlag and frFlag, each associated with a file Blob.

    inlineImages:
    {
    nlFlag: nlFlagBlob,
    frFlag: frFlagBlob
    }
  • File blobs can be obtained in many ways; here we've used UrlFetchApp.fetch() to get the binary images from WikiMedia, then converted them to blobs and set the name of the blob for attaching.

Code:

/**
* Example of sending an HTML email message with inline images.
* From: http://stackoverflow.com/a/37688529/1677912
*/
function sendInlineImages() {
var mailaddress = Session.getActiveUser().getEmail();
var subject = "test inline images";
var bodyNL = "This is <B>DUTCH</B> text.";
var bodyFR = "This is <B>FRENCH</B> text.";

// Image URLs, under CC license
var nlFlagUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Flag_of_the_Netherlands.png/320px-Flag_of_the_Netherlands.png";
var frFlagUrl = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/320px-Flag_of_France.svg.png";

// Fetch images as blobs, set names for attachments
var nlFlagBlob = UrlFetchApp
.fetch(nlFlagUrl)
.getBlob()
.setName("nlFlagBlob");
var frFlagBlob = UrlFetchApp
.fetch(frFlagUrl)
.getBlob()
.setName("frFlagBlob");

// Prepend embeded image tags for email
bodyNL = "<img src='cid:nlFlag' style='width:24px; height:16px;'/>" + bodyNL;
bodyFR = "<img src='cid:frFlag' style='width:24px; height:16px;'/>" + bodyFR;

// Send message with inlineImages object, matching embedded tags.
MailApp.sendEmail(mailaddress, subject, "",
{ htmlBody: bodyNL + "<BR/><BR/>" + bodyFR,
inlineImages:
{
nlFlag: nlFlagBlob,
frFlag: frFlagBlob
}
});

}


Related Topics



Leave a reply



Submit