Including images in emails

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
andyh1234
Posts: 412
Joined: Mon Aug 13, 2007 4:44 pm
Location: Sale UK / FL USA
Contact:

Including images in emails

Post by andyh1234 » Sun Jul 14, 2013 12:23 pm

Here goes!

I have a script that I use to ask users to authenticate their email when they first sign up that sends them an email, they then click on a link in the same email that comes back to the server and authenticates the email address, all good.

I can send the email in both plain and html formats, and if I wanted to Ive figured out how to include attachments, but...

I would like to embed a small header image into the email. I know I can just link to the image on the server, but a lot of email programs then fail to open the image as spammers use the same technique to track receipt of their spam, so its best to include the image in the actual email.

For the life of me however I cannot get it to work with Livecode, any ideas???

andyh1234
Posts: 412
Joined: Mon Aug 13, 2007 4:44 pm
Location: Sale UK / FL USA
Contact:

Re: Including images in emails

Post by andyh1234 » Sun Jul 14, 2013 12:25 pm

Sorry, just to add, the help topic at...

http://lessons.runrev.com/s/lessons/m/4 ... er-scripts

covers all the basic email, html email and attachments for anyone interested in just sending email, I only opened this thread in case anyone has any ideas on how to embed images into a html email using livecode server.

Thanks!

andyh1234
Posts: 412
Joined: Mon Aug 13, 2007 4:44 pm
Location: Sale UK / FL USA
Contact:

Re: Including images in emails

Post by andyh1234 » Mon Jul 15, 2013 4:16 pm

I believe I've found the answer to this, so I'm posting it in case it helps anyone else!

If you follow the instructions on the main Livecode site using the link in the last post, this works for text and html emails.

To extend this to embed images inline in the html, I needed to modify the following block...

Code: Select all

    -- add each attachment as a new part of the message, sepearting using 
    -- the generated boundary
    if pAtts is an array then
        put "--" & tBoundary & return after tMsg
        repeat for each element tAtt in pAtts
            if there is a file tAtt["path"] then
                if tAtt["type"] is empty then
                    get "application/octet-stream"
                else
                    get tAtt["type"]
                end if
                
                put "Content-Location: CID:" & tAtt["name"] && ";" & return after tMsg
                put "Content-ID: <" & tAtt["name"] & ">" & return after tMsg
                
                put "Content-Type:" && it & "; name=" & wrapQ(tAtt["name"]) & ";" & \
              return & "Content-Transfer-Encoding: base64" & return & return & \
                base64Encode(URL ("binfile:" & tAtt["path"])) & return & "--" & \
                tBoundary & return after tMsg

            end if
        end repeat
    end if
The two lines that are different are the 'content-location' and 'content-id'.

Then, in the mail construction you simply attach the images as attachments, e.g....

put "./xyz.png" into tAttsA[1]["path"]
put "xyz.png" into tAttsA[1]["name"]
put "img/png" into tAttsA[1]["type"]

and then in the message reference the name with CID: before it, eg...

put "<h1>This is a test message.</h1><IMG SRC='cid:xyz.png' ALT='Test Image'><p>The image should appear above this line.</p>" into tMsg

Then, just call the mail command as detailed on the main site, with the two modified lines and the email gets sent with the image embedded inline.

I hope this helps someone else, now all thats left is to embed a text alternative to the html!

Post Reply

Return to “CGIs and the Server”