I found the trick I am about to explain on the web. When the sales manager told me to create a procedure to send newsletter through the company Intranet web site, there was really no problem (I believe almost everyone knows how to do that!). It really got challenging when he told me they wanted to send images attached to the message body. I knew how to attach documents, but embedded images? Hmmm... that was tricky. Then I found the solution.
Let's start with the code:
<%
Dim objCDO
Set objCDO = Server.CreateObject("CDO.Message")
objCDO.From = varsender 'Senders Email Address
objCDO.To = varrecipient 'Recipient Email Address
objCDO.CC = varcc 'Carbon Copy Address
objCDO.BCC = varccn 'Carbon Copy Address
objCDO.Subject = varsubject 'Email Subject
varimage = "<html>"& vartext &"<br><br><img src=""cid:" & Request("varbackground") & """></html>"
if Request("varbackground") <> "" then
objCDO.HTMLBody = varimage 'Email Message
' Here's the good part, thanks to some little-known members.
' This is a BodyPart object, which represents a new part of the multipart MIME-formatted message.
' Note you can provide an image of ANY name as the source, and the second parameter essentially
' renames it to anything you want. Great for giving sensible names to dynamically-generated images.
Set objBP = objCDO.AddRelatedBodyPart(Server.MapPath(varbackground), varbackground, CdoReferenceTypeName)
'Set objBP = objCDO.AddRelatedBodyPart(Server.MapPath("/images/myimage.gif"), "myimage.gif", CdoReferenceTypeName)
' Now assign a MIME Content ID to the image body part.
' This is the key that was so hard to find, which makes it
' work in mail readers like Yahoo webmail & others that don't
' recognise the default way Microsoft adds it's part id's,
' leading to "broken" images in those readers. Note the
' < and > surrounding the arbitrary id string. This is what
' lets you have SRC="cid:myimage.gif" in the IMG tag.
objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<"& varbackground &">"
objBP.Fields.Update
else
objCDO.HTMLBody =vartext
end if
objCDO.Send() 'send mail
Set objCDO = Nothing 'Clean up your objects!!!
%>
The comments are the original ones and the forum where I found this little gem is here:
http://support.jodohost.com/archive/index.php?t-7692.html
What I did is just to fit the code to my needs. If you see I added an If.. Then... Else statement just to verify the presence of an image to embed, because our people needs to send newsletter with or without embedded images. The other trick was to create a procedure to upload images to be embedded, and an interactive form where you select the image to send (among all the other usual fields). But again that's another story.
Great job indeed. Thanks to page I am now one step ahead. Ir Works great. But I have a strange problem. When I added embeded image, the content of textbody properties (text portion) is lost. I see only the image I have embeded. I want to see the image at the end of the mail as a signature image. Any comment please?
ReplyDeleteHakan
vartext is where the text part of the email stays. Careful! It's HTML.
DeleteThanks Marco,
DeleteI'm geting error which says "undefined sub/function [Request])". What is the REQUEST function?
You should build a form. The Request is used to gather the info posted by the form.
DeleteYes, I have modified the code to my need and it work fine. I am using VB6 and if you want I can post my code here for a reference who may need.
ReplyDeleteJust a little small problem I have now as I don't know HTML coding.
I have a textbox in my form to add text for body text. When I write some information more then one line in that textbox, I receive the mail in one row like this.
The textbox content like this
Dear Sir,
Please find enclosed offer.
Best regards,
The amil I receive is;
Dear Sir,Please find enclosed offer.Best regards.
Your kind help will make my day.
Thanks
Hakan
Because it's HTML. You need a line break:
Delete...like a br tag < br > (without spaces)
DeleteI have built a code for converting the linebreaks in a textbox to HTML. And now, everything seems ok. Thanks for your kind support Marco.
ReplyDeleteBest regards,
Here is the code:
Public Function TexttoHTML(Bilgi As String) As String
k = 1
k = InStr(k, Bilgi, Chr(13) & Chr(10))
Do While k > 0
Bilgi = Left(Bilgi, k - 1) & "
" & Right(Bilgi, Len(Bilgi) - k - 1)
k = InStr(k + 2, Bilgi, Chr(13) & Chr(10))
Loop
TexttoHTML = Bilgi
End Function
Great! Thanks.
DeleteI've just noticed that, in the above code at the and of line 5, the tag
Deleteis not showing. I think your page assumed it as a linebreak and so splited the line. :)
Hello Marco,
DeleteHave ever tried to use CC and Bcc fields? Because it seems not working. I have put valid email adress in both fields and didn't work. I am getting no error but the result is not as expected.
Hakan
Yes they did work.
DeleteI have tried .bcc=" instead of .bcc="xxx@blabla.com" and worked. Do you have any idea of this notation in VB?
DeleteHakan
Try to use ' instead of "
DeleteI have tried .bcc="< xxx@blabla.com >" instead of .bcc="xxx@blabla.com" and worked. Do you have any idea of this notation in VB?
ReplyDeleteHakan
' means user comment in VB like // in C language. The right of the ' sign is assumed as comments by the compiler. "< xxx@aaa.com > works fine now. I accepted it as a compiler rule and happy. :)) Thanks again
ReplyDeleteHi Marco,
ReplyDeleteIt's time to teach us how to receive mail with CDO (subject, body, attacments, images etc), isn't it? Hope you won't forsake us. :)
Hakan
Good try Hakan! I'm sure you know you can't receive emails with CDO...
DeleteOk then, let me change my question. It's time to teach us the way of coding to receive emails, isn't it?
ReplyDeleteTake care
Hakan
How can we add "request read receipt" from the receiver while sending email with CDO?
ReplyDeleteHakan
You can't...
Delete