0

The code below was posted by HK1 in response to an answer on sending email without Outlook in VBA, dated 20 Jul 12.

The code works well, but I need to add a signature block at the end of the text (basically a jpg file in a local folder), but the best I have been able to come up with is to add the path (text) instead of the image itself to the email body.

Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM

Public Sub SendEmail()
 Dim imsg As Object
 Dim iconf As Object
 Dim flds As Object
 Dim schema As String

 Set imsg = CreateObject("CDO.Message")
  Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields

' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "mail.myserver.com"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "email@email.com"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update

With imsg
    .To = "email@email.com"
    .From = "email@email.com"
    .Subject = "Test Send"
    .HTMLBody = "Test"
    '.Sender = "Sender"
    '.Organization = "My Company"
    '.ReplyTo = "address@mycompany.com"
    Set .Configuration = iconf
    .Send
End With

   Set iconf = Nothing
   Set imsg = Nothing
   Set flds = Nothing
End Sub

I tried amending the code as follows, but this simply adds the file path to the body text:

With imsg
  .To = vRecipients
  .From = senderEmail
  .CC = vCC
  .Subject = vSubject

  vBody = Replace(vBody, vbCrLf, "<br>")
  vBody = "<FONT face=arial size=2>" & vBody

  vBody = vBody & "<br>" & signFile
  .HTMLBody = vBody

  .Sender = senderName
  .ReplyTo = senderEmail
  .AddAttachment vAttachments

  Set .Configuration = iconf
  .Send
End With

Any suggestions?

Erik A
  • 31,639
  • 12
  • 42
  • 67
PeterJ
  • 364
  • 4
  • 17
  • So `signFile` contains only the filename of the signature file. Then you have to read the contents of the file and add that to the body. – dwo Sep 26 '13 at 20:33

1 Answers1

1

dwo is correct. You need to use a File System Object or a File Object to read in the text contents of your signFile. Otherwise your code looks like it should work.

Here's a function you can use to read the contents of a file. The function simply assumes that you'll pass in the entire path and file name for a text file that your application has at least read rights to.

Public Function GetTextFileContents(sFilePath as String) As String
    If Dir(sFilePath) <> "" Then
        Dim fso As Object
        Dim ts As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(sFilePath).OpenAsTextStream(1, -2)
        GetTextFileContents = ts.ReadAll
        ts.Close
        Set ts = Nothing
        Set fso = Nothing
    End If
End Function
HK1
  • 11,941
  • 14
  • 64
  • 99
  • I tried reading in the file contents and replace signFile with GetTextFileContents, but the image does not show up in the email, instead it shows ÿØÿà, which I guess is part of the binary content? – PeterJ Sep 27 '13 at 18:18
  • Images in signatures are a complicated problem. I resorted to telling my users that it wasn't feasible to include them. I don't think the image is actually stored in the signature. I think it might be a relative file path but I don't remember. You can find out by manually opening the signature file using notepad. It should be an .HTML file. Search Google for the place where Outlook stores signatures. – HK1 Sep 27 '13 at 19:00
  • Actually, the signature file is simply a jpg with the logo and some contact details. So I guess what it boils down to is how to include an image in the body when using CDO.Message? – PeterJ Sep 27 '13 at 19:39
  • I won't say it can't be done but the only way I've ever included an image in the body was using Outlook. And even then it wasn't exactly easy. – HK1 Sep 27 '13 at 19:54
  • If you can put your logo online at some website that allows hot-linking then you should be able to use an html img tag. See this post: http://stackoverflow.com/questions/10904763/asp-embed-image-to-cdo-message-email – HK1 Sep 27 '13 at 22:05