PeterJ
PeterJ

Reputation: 373

Include an email signature in

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 protected]"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update

With imsg
    .To = "[email protected]"
    .From = "[email protected]"
    .Subject = "Test Send"
    .HTMLBody = "Test"
    '.Sender = "Sender"
    '.Organization = "My Company"
    '.ReplyTo = "[email protected]"
    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?

Upvotes: 0

Views: 1961

Answers (1)

HK1
HK1

Reputation: 12220

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

Upvotes: 1

Related Questions