[SOLVED] Sending HTML email with embedded image
lazarus / 61-Beginners
----------------------------------------------------------------------
tudi_x · 2015-06-23 13:12
Hi All,
I need to send HTML email with embedded PNG image in order for Outlook
not to block the image.
As I understand the HTML reference would be something like '
' instead of '
'
Please advise how would this be possible.
Thank you
Lazarus 1.4 32b Win7 64b
----------------------------------------------------------------------
skalogryz · 2015-06-23 13:25
Synapse library can work for you.
You'd need to assemble an email body, with content-type multipart-
related
The image attachment should come with "inline" disposition and the
Content-ID that would be used in "img" tag.
Sounds fun, right?
----------------------------------------------------------------------
skalogryz · 2015-06-23 13:33
To be more specific about Synapse library.
For email body (aka mime-messages) it has a nice class TMimeMess. It
would allow you to construct the body and set all attachments right
(with desired IDs, depositions etc)
However, the body is only the first part, since then you'd need to
send out the email, using TSMTPSend class
----------------------------------------------------------------------
tudi_x · 2015-06-23 20:00
Thank you for pointing me in the Synapse direction.
The below worked for me, I used the example from
http://stackoverflow.com/questions/20973354/how-to-send-a-email-with-
a-base64-embedded-image .
Code: [Select]USES
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
smtpsend, mimemess, mimepart, synautil;
....
FUNCTION TForm1.email(AText, AFilePath, AFrom , ATo, ASubject :STRING)
: BOOLEAN;
VAR
message : TMimemess;
part : TMimepart;
related_part : TMimepart;
image_part : TMimepart;
body_html: TStringList;
cid : string;
BEGIN
TRY
cid := AFilePath;
message:= TMimemess.Create;
body_html:= TStringList.Create;
body_html.Append('');
body_html.Append('
');
body_html.Append('' + AText + '
');
body_html.Append('');
body_html.Append('
');
part := message.AddPartMultipart('mixed', nil);
related_part := message.AddPartMultipart('related', part);
message.AddPartHTML(body_html,part);
image_part := message.AddPartHTMLBinaryFromFile(AFilePath,
cid, related_part);
related_part.AddSubPart;
related_part.AssignSubParts(image_part);
message.header.from:= AFrom;
message.header.tolist.add(ATo);
message.header.subject:= ASubject;
message.EncodeMessage;
Result := SendToRaw(AFrom, message.Header.ToList.Text,
'127.0.0.1' , message.Lines, '', '');
FINALLY
message.Free;
body_html.Free;
END;
END;
As a note Papercut did not render the email / image on my '127.0.0.1'.
----------------------------------------------------------------------
tudi_x · 2015-06-23 21:15
And for many images this worked for me:
Code: [Select]related_parts : ARRAY OF TMimepart;
image_parts : ARRAY OF TMimepart;
part := message.AddPartMultipart('mixed', nil);
related_parts[i] := message.AddPartMultipart('related', part);
image_parts[i] := message.AddPartHTMLBinaryFromFile('image_i',
'image_i', related_parts[i]);
related_parts[i].AddSubPart;
related_parts[i].AssignSubParts(image_parts[i]);
----------------------------------------------------------------------
donaldklopper · 2015-09-07 10:36
Thank you skalogryz and tudi_x your suggestions worked well for me.
I'm quite in awe of the work done around Synapse. It caters for my
needs for SSL and attachments, as well as HTML formatted mail body.
The resolve the additional plaint-text body need I've come across this
forum thread on www.trichview.com, url in the form
[slash]forums[slash]viewtopic.php?t=6133
(sorry for the weird url... I don't know whether this forum allows
posting of URL's)
----------------------------------------------------------------------
donaldklopper · 2015-09-07 10:37
Lemme try that URL:
http://www.trichview.com/forums/viewtopic.php?t=6133
----------------------------------------------------------------------
fuchaka · 2017-01-12 11:34
{
Assumed Environment
Lazarus 1.6.2 32-Bit
Indy 10
Windows 10
//html message_text to contain the following line:
}
var
//these variables should be initialized with correct values for the
code to work
from_text, subject_text : string;
email_address, message_text : TStringlist;
email_username, email_password : string;
email_host : string; email_port : integer
//these variables to be used in the sending procedure
Msg: TIdMessage;
DestAddr: TIdEmailAddressItem;
id_text : TidText;
begin
Msg := TIdMessage.Create(Self);
with TIdText.Create(Msg.MessageParts, nil) do
begin
ContentType:= 'multipart/alternative';
end;
id_text := TIdText.Create(Msg.MessageParts, nil);
with id_text do
begin
Body.Text:= message_text.Text;
ContentType:= 'text/html';
ParentPart:= 0;
end;
if FileExists('logo_small.jpg') then
begin
with TIdAttachmentFile.Create(Msg.MessageParts, 'logo_small.jpg')
do
begin
ContentID:= '';
ContentType:= 'image/jpg';
FileName:= 'logo_small.jpg';
end;
end;
Msg.ContentType:= 'multipart/related';
//send the message itself
Msg.From.Text := from_text;
Msg.From.Address := email_username;
Msg.Subject := subject_text;
DestAddr := Msg.Recipients.Add;
DestAddr.Text := 'restive98';//:-)
DestAddr.Address := trim(email_address.Text);
IdSMTP1.Host := email_host;
IdSMTP1.Port := email_port;
IdSMTP1.AuthType:= satDefault;
IdSMTP1.Username := email_username;
IdSMTP1.Password := email_password;
try
if trim(user_email.Text) <> '' then
begin
if IdSMTP1.Connected = True then
begin
IdSMTP1.Disconnect();
end;
IdSMTP1.Connect;
IdSMTP1.Authenticate;
IdSMTP1.Send(Msg);
IdSMTP1.Disconnect;
end;
//email send was successful clap your hands
except on E:Exception do
begin
//log your damn errors here
end;
end;
id_text.Free;
Msg.Free;
end;
----------------------------------------------------------------------
Remy Lebeau · 2017-01-12 18:42
Quote from: tudi_x on June 23, 2015, 03:12:04 pmI need to send HTML
email with embedded PNG image in order for Outlook not to block the
image.
As I understand the HTML reference would be something like '
' instead of '
'
That is correct.
Quote from: tudi_x on June 23, 2015, 03:12:04 pmPlease advise how
would this be possible.
As fuchaka mentioned, Indy can also be used for HTML emails. Its
TIdMessage component can be used to create the email, and its
TIdMessageBuilderHtml class can help populate the TIdMessage
correctly. I even wrote a couple of blog articles about this topic on
Indy's website:
HTML Messages
New HTML Message Builder class
----------------------------------------------------------------------
Remy Lebeau · 2017-01-12 18:48
Quote from: fuchaka on January 12, 2017, 12:34:43 pmCode: [Select]with
TIdText.Create(Msg.MessageParts, nil) do
begin
ContentType:= 'multipart/alternative';
end;
fuchaka, since you are not providing a "text/plain" part, you don't
need to wrap the "text/html" part inside of a "multipart/alternative"
part, since there is no alternative content being provided. What is
important is that the "text/html" and "image/jpg" parts are inside of
the "multipart/related" part. I cover this in the blog articles I
mentioned above.