1

I know this topic has been brought up many times but not specifically for Delphi. I am trying to send generated qr codes as inline img via mail. Currently i am succeeding in doing so but only as URL :

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABqQAAAakCAYAAA..." width="170" height="170">

But when it comes to Gmail it only adds an empty square. I have looked up many answers how to embedde an img to email such as this 2 posts:

Base64 images to gmail

Gmail blocking small embedded inline images in email template

But i do not understand how to implement the code in answers inside Delphi.

I also found code that gets the img as attachment from disk:

procedure TForm1.Button1Click(Sender: TObject);
var
 oSmtp : TMail;
 cid : WideString;
begin
 oSmtp := TMail.Create(Application);

 // Add embedded image and return the unique identifier of the attachment
 cid := oSmtp.AddInline('c:\test.jpg');

 // Set HTML body format
 oSmtp.BodyFormat := 1;
 // Set HTML body
 oSmtp.BodyText := '<html><body>Hello, this is an embedded <img src="cid:' +
 cid + '"> picture.</body></html>';
end;

// i do not use this code it is just an example of what i have found thus far

Currently all i am doing with img is this:

  HtmlBody = ReplaceStr(HtmlBody, [parameter_img],'<img src="data:image/png;base64,' + StreamToString(imgStream) + '"width="170" height="170"/>');

But i can not save the imgs so i want to insert the base64string directly.

UPDATE

I have managed to get the attachment in my email and it is there but in the text area there is only a blank square.

the img part of the code and original email msg:

<img src="cid:qrcode.png" width="170" height="170" />';
--wpn=_6hoco9kAT3gWlDy6D313EA3BETyyOfe
Content-Type: text/html; charset="windows-1250"
Content-Transfer-Encoding: base64
Content-Disposition: inline

PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIiAiaHR0cDovL3d3dy53My5vcmcvVFIvUkVDLWh0bWw0MC9sb29zZS5kdGQiPjxtZXRhIGh0
dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXdpbmRv
d3MtMTI1MCI+PGJvZHk+PGltZyBzcmM9Imh0dHBzOi8vbmFpcy1yYXp2b2ouaXBsdXMuc2kvX2Fz
...

--wpn=_6hoco9kAT3gWlDy6D313EA3BETyyOfe
Content-Type: image/png; name="qrcode.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="qrcode.png"
Content-ID: qrcode.png


--wpn=_6hoco9kAT3gWlDyD6D313EA3BEyyOfe--
BrianOrion
  • 59
  • 7
  • It looks like you use [EOSendMail](https://www.emailarchitect.net/easendmail/sdk/) to create mail messages. Answers to the questions you listed guide you to add the image as a regular attachment and use its [ContentID](https://www.emailarchitect.net/easendmail/sdk/html/attachment_contentid.htm) property to construct CID URL that you can use as `src` of `img` tag inside the message body. – Peter Wolf May 09 '22 at 15:05
  • You sure can save pictures temporarily in a file, use any method that requires a filename, and then delete the temporary file again. Otherwise look at the source of `.AddInline()` and trace down how to do it/call it without the need to access a file. – AmigoJack May 09 '22 at 16:39

1 Answers1

1

At first i thought the issue is this : PCFET0NUWVBFIGh0bWwgUFVCTElDICItL since it does not look like base64 code of png which starts with iVBOR.

But the issue was here : Content-ID: qrcode.png

When setting Content-ID you have to be carefull since in some versions simply setting it with quotation marks is not enough and you have to set it with adding <> like so : Content-ID: <qrcode.png>.

With this the result was as wanted.

BrianOrion
  • 59
  • 7
  • This is explained in [RFC 2392 from 1998](https://datatracker.ietf.org/doc/html/rfc2392). Also the ID is not required to resemble a filename - it could be as simple as `Content-ID: <001>` and then using it per ``, which even avoids characters that possibly need escaping. – AmigoJack May 15 '22 at 11:05