Reputation: 1127
I have a macro that parses a spreadsheet and compiles emails based on parsed data. The problem that I'm facing is the visual formatting. When I use the macro to send the email to an outlook email address, everything works perfectly (html code works), but when I send the same email to, say, gmail web client, I'm losing the html and my email is sent in plain text. This is what my email function looks like:
Function OutlookEmail(SendTo As String, subject As String, Message As String, send As Boolean) Dim Outlook As Object, NewMail As Object, SafeItem As Object, Attach As Object Set Outlook = CreateObject("Outlook.Application") Set SafeItem = CreateObject("Redemption.SafeMailItem") Set NewMail = Outlook.CreateItem(0) SafeItem.Item = NewMail With SafeItem .Recipients.Add SendTo .Recipients.ResolveAll .subject = subject .HTMLBody = Message End With If send = True Then SafeItem.send If send = False Then SafeItem.display Set Outlook = Nothing Set NewMail = Nothing Set SafeItem = Nothing Set Attach = Nothing End Function
Redemption is an email plugin that helps send everything automatically and Message contains HTML and inline-css formatted text.
Here's what it looks like when sent to an outlook client:
Other than below, it's a simple loop with some formatting. CSS vars:
Dim CSS_p As String, CSS_phone As String, CSS_table As String, CSS_th As String, CSS_td As String
Dim CSS_first As String, CSS_last As String, CSS_jlist As String, CSS_warning As String, CSS_accent As String
CSS_p = " style='line-height: 16px;color: #666;margin-top: 10px; margin-bottom: 15px; margin-left: 20px; margin-right: 10px;'"
CSS_phone = " style='font-weight: bold;'"
CSS_table = " style='font-size: 14px; text-align: center;'"
CSS_th = " style='padding: 0 0.5em; text-align:center; " & _
"border-top: 1px solid #FB7A31; border-bottom: 1px solid #FB7A31; background-color: #FFC;'"
CSS_td = " style='border: 1px solid #CCC;padding: 0 0.5em; text-align:center;'"
CSS_first = " style='border-top: 1px solid #CCC; border-bottom: 1px solid #CCC; border-left: none; border-right: 1px solid #CCC'"
CSS_last = " style='border-top: 1px solid #CCC; border-bottom: 1px solid #CCC; border-left: 1px solid #CCC; border-right: none;'"
CSS_jlist = " style='border-collapse:collapse;line-width:1px;'"
CSS_warning = " style='line-height: 16px;color: red;margin-top: 10px; margin-bottom: 15px; margin-left: 20px; margin-right: 10px;'"
CSS_accent = " style='text-decoration:underline; font-weight:bold;'"
Table content:
job_list = job_list & "<tr><td" & CSS_td & ">" & account & "</td>" & _
"<td" & CSS_td & ">" & location & "</td>" & _
"<td" & CSS_td & ">" & address & "</td>" & _
"<td" & CSS_td & ">" & work_order & "</td>" & _
"<td" & CSS_td & ">" & description & "</td>" & _
"<td" & CSS_td & ">" & service_date & "</td></tr>"
Message:
email_body = "<html><head></head>" & _
"<body><p" & CSS_p & ">" & Greeting() & _
"</p>" & _
"<p" & CSS_p & "> Our records indicate that we have not received signed paperwork for the " & _
"following jobs as of yet:</p>" & _
"<table" & CSS_table & ">" & _
"<th" & CSS_th & ">Account</th>" & _
"<th" & CSS_th & ">Store Number</th>" & _
"<th" & CSS_th & ">Address</th>" & _
"<th" & CSS_th & ">Work Order#</th>" & _
"<th" & CSS_th & ">Description</th>" & _
"<th" & CSS_th & ">Service Date</th>" & _
job_list & "</table>" & _
"<p" & CSS_p & ">Please remember to submit the work orders, your invoices and checklists (if applicable) for the jobs " & _
"referenced above." & _
"<p" & CSS_p & ">You may send the paperwork via:</p>" & _
"<ul><li>Fax to (000) 000 0000 or,</li>" & _
"<li>Email to: [email protected] (please indicate your crew code in the email subject)</li></ul>" & _
"<p" & CSS_p & ">All work orders must be signed and stamped by the store manager on duty at the time of service. If the " & _
"stamp is not available, please ask the manager to write that on the work order (and checklists if applicable), as well as " & _
"initial the note. Submitting un-signed and un-stamped paperwork, that is not properly filled out, may result in your " & _
"invoices being rejected.</p>" & _
"<p" & CSS_p & ">Thank you for your cooperation.</p>" & _
HTMLSignature & "</body></html>"
OutlookEmail SendTo:=email, subject:=email_subject, Message:=email_body, send:=True
Upvotes: 2
Views: 3888
Reputation: 2089
You are forgetting to set the body format of the email msg to HTML, Outlook is really good at autodetecting these things.
set BodyFormat to olFormatHTML Witch I think is the value 2
I don't have redemtion on this machine so I can check but it should be somthing like
With SafeItem
.Recipients.Add SendTo
.Recipients.ResolveAll
.subject = subject
.HTMLBody = Message
.BodyFormat = olFormatHTML ' or the value 2
End With
Upvotes: 0