Sending email with image in the body

Xbase++ 2.0 Build 554 or later
Message
Author
User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Sending email with image in the body

#1 Post by rdonnay »

I'm trying to figure out how to use SmtpMail to build a body of text that embeds an image which will be viewed by Outlook.

I know that Outlook rejects Base64 images, so I will need to have a link to an image on my server.

However, I don't know how to build the body of text to make this happen.

HTML doesn't seem to work.
The eXpress train is coming - and it has more cars.

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Sending email with image in the body

#2 Post by rdonnay »

It looks like I may have found something in Xbase++ sample program: c:\users\roger\documents\xbase++\source\samples\asinet\htmlmail\mailmain.prg
The eXpress train is coming - and it has more cars.

skiman
Posts: 1195
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: Sending email with image in the body

#3 Post by skiman »

Hi Roger,

I don't know if this can help. I'm using Chilkat and there I have the following to embed images.

oEmail:AddRelatedFile2(cImage, cImageID)

cImage is the source of the image. This is a local path as c:\chris\image.jpg.
cImageId is a name/ID for the image as image001.
In the message i have a strtran(cMessage,cImage,cImageID)

With the addRelatedFile2 a mail client shows the image in the mail text imbedded.
Best regards,

Chris.
www.aboservice.be

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Sending email with image in the body

#4 Post by rdonnay »

Thanks Chris, but it looks like Alaska's htmlmail sample has a lot to work with, however it is way to complex for such a simple problem.

I'm going to rewrite it to simplify it.
The eXpress train is coming - and it has more cars.

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Sending email with image in the body

#5 Post by rdonnay »

Alaska's sample program is way too complicated for such a simple task.

I made a small modification to the SENDMAIL.PRG to change the content-type to "text/html"

http://bb.donnay-software.com/donnay/sendmail.prg

I put the below HTML into the body of the email and it worked perfectly:

Code: Select all

<html>
<body>
<h1>
Testing HTML embedded images
</h1>
<h3>Click on image below to register to drive for TaxiFleet NYC</h3>
<a href="https://drivebigapple.taxi/apply-to-drive/">
<img src="http://70.23.98.154:8082/taxifleetlogo.jpg">
</a>
</body>
</html>
The eXpress train is coming - and it has more cars.

skiman
Posts: 1195
Joined: Thu Jan 28, 2010 1:22 am
Location: Sijsele, Belgium
Contact:

Re: Sending email with image in the body

#6 Post by skiman »

Hi Roger,

The difference is that when the image is an external URL the image won't be shown, unless the user clicks to load the image. With the Chilkat method, the image is embedded in the mail, and is always shown.
I looked at the Xbase++ documentation and I don't know if there is any method available to embed the images.

Otherwise, with an external image you can track how many times it is downloaded. This isn't possible with the embedded image.

So both systems have their pro's and con's.

We use the external image to send a pixel in our mails with invoices. If they open the mail and they allow the images, we can see who has opened the mail. The 'src' of the image is a rest-endpoint which returns the image, but has the data we need in the request as ....:8082/getimage?CusItD=123&invoiceid=987. This way we know that customer 123 has opened the mail for the invoice 987. Then we return the image.
Best regards,

Chris.
www.aboservice.be

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Sending email with image in the body

#7 Post by rdonnay »

the image is an external URL the image won't be shown, unless the user clicks to load the image
With the HTML body I used, the image is shown without a need to click.
The eXpress train is coming - and it has more cars.

User avatar
SlavkoDam
Posts: 111
Joined: Wed Apr 27, 2022 10:12 am
Location: Negotin, Serbia
Contact:

Re: Sending email with image in the body

#8 Post by SlavkoDam »

Roger,

In SmtpMail message, an embedded image in a HTML file is attached with MimeContent():attachedRelated(oMimeContent,cID) method. Here is an Alaska example how to do that.

Code: Select all

PROCEDURE main( cEmailAdr, cMailServer, cUsername, cPassword )
       LOCAL oMe, oMail, oServer, oBitmapCont
       LOCAL cID

       cID := MimeContent():new():uniqueString
       oMe := MAILAddress():new( cEmailAdr )

       oMail := MIMEMessage():encodeQuotedPrintable( getHtml(cEmailAdr, cID), "text/html" )
       oMail:setFrom( oMe )
       oMail:addRecipient( oMe )
       oMail:setSubject( "Asinet Html mail docu sample" )

       oBitmapCont := getBitmapCont()

       IF ! Empty( oBitmapCont )
          oMail:attachRelated( oBitmapCont, cID )
       ENDIF

       oServer := SMTPClient():new( cMailServer )
       IF ! oServer:connect( cUsername, cPassword )
          ? "Unable to connect to mail server"
          WAIT
          QUIT
       ENDIF

       oServer:send( oMail )
       oServer:disconnect()
       ? "Message sent!"
       ? "Check account", cEmailAdr, "for new mail"
       wait
   RETURN

   FUNCTION getBitmapCont()
      LOCAL cXppResource, oContent, nPos

      cXppResource := Lower( GetEnv( "XppResource" ) )
      IF 0 == ( nPos := At( "resource", cXppResource ) )
         ? "XppResource Environment variable not set:"
         ? "  Background image can not be loaded"
         RETURN NIL
      ENDIF
      cXppResource := SubStr( cXppResource, 1, nPos + 7 )
      cXppResource += "\bitmap\keybd1.bmp"

      oContent := MimeContent():createFromFile( cXppResource )
   RETURN oContent

   FUNCTION getHtml( cMail, cID )
      LOCAL cStr := ""
      
      cStr += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
      cStr += '<html>'
      cStr += '  <head></head>'
      cStr += '  <body background="cid:' + cID + '"'
      cStr += '    <h1>'
      cStr += '       Hallo from <br>'+cMail+'!'
      cStr += '    <h1>'
      cStr += '  </body>'
      cStr += '</html>'
   RETURN cStr
Best regards,

Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Sending email with image in the body

#9 Post by rdonnay »

Here is an Alaska example how to do that.
I have looked at the Alaska HTMLMAIL sample and I see no reason to do anything other than the simple example I posted which is working perfectly for me.
The eXpress train is coming - and it has more cars.

User avatar
SlavkoDam
Posts: 111
Joined: Wed Apr 27, 2022 10:12 am
Location: Negotin, Serbia
Contact:

Re: Sending email with image in the body

#10 Post by SlavkoDam »

I'm trying to figure out how to use SmtpMail to build a body of text that embeds an image which will be viewed by Outlook.
This is your initial goal that you wanted to achieve. You code is simpler but the image is not embedded and its not the solution for what you wanted in the above. I gave you the solution for the above task (your code don't do that), but its up to you what are you really looking for. The image sent by your example will be seen by the recipient while its present on the specified address. When you remove it from your server the recipient will not see it any more, since the image is not embedded in the mail body. With my solution the image will be present in the mail body for good, but its up to your what you really want. If you didn't want the embedded image, you shouldn't ask the solution for that.

Anyway, its good for you to learn and know how to embed an image in the mail body with SmtpClient(). There are also useful methods MIMEContent():attachAltermative() and MIMEContent():attachMixed(). You should study them by yourself, there are Alaska examples how to use them.
Best regards,

Slavoljub Damnjanovic
SD-SoftDesign, Alaska Software Technology Partner
https://www.sd-softdesign.com
https://www.sd-softdesign.rs

Post Reply