51
General Software Discussion / Best Script type to build an email body line by line
« on: March 10, 2017, 01:55 AM »
This is something that might have other solutions but if it can be done in a single script it would be handy. VBS PS Batch whatever.
the content is a list of hyperlinks that need to be emailed. Currently I do it by building an RTF file which then has to be zipped to be attached to an email.
i have it all working but would like to remove the extra steps required at each end for zipping and unzipping as well as making each written hyperlink active when clicked.
I am now building the .rtf file line by line in a separate script and it would be helpful to instead read each line into the body of the email that is used to send it.
Currently, i am using a VBS script to get to where i go now. Once i get to the steps that open Outlook and begin the email, i would need to add the correct vbs code to read the hyperlinks from one location and paste them into the email body then go to the next line for the next hyperlink.
The end result would be a list of maybe 40 hyperlinks which the end of the VBS script adds the recipient's name and sends it on its way.
The code below works by me dropping the zipped file onto the VBS script on my desktop to initiate and send the email.
It works fine that way except for the zipping /unzipping, etc
Is there a way to add the proper code to create the content for the body at step "oEmailItem.BodyFormat = olFormatHTML"?
What would be the proper statement to add to the VBS to loop through a list if hyperlinks and write them to the email body?
Currently the first script i use echoes the variables to the .rtf file. I tried loading the contents of the entire .rtf file to the email body but it loses all the layout and everything runs together.
Thanks for any ideas. Note that i the code below i used "examples" for the recipient but the rest is pretty much working as is.
the content is a list of hyperlinks that need to be emailed. Currently I do it by building an RTF file which then has to be zipped to be attached to an email.
i have it all working but would like to remove the extra steps required at each end for zipping and unzipping as well as making each written hyperlink active when clicked.
I am now building the .rtf file line by line in a separate script and it would be helpful to instead read each line into the body of the email that is used to send it.
Currently, i am using a VBS script to get to where i go now. Once i get to the steps that open Outlook and begin the email, i would need to add the correct vbs code to read the hyperlinks from one location and paste them into the email body then go to the next line for the next hyperlink.
The end result would be a list of maybe 40 hyperlinks which the end of the VBS script adds the recipient's name and sends it on its way.
The code below works by me dropping the zipped file onto the VBS script on my desktop to initiate and send the email.
It works fine that way except for the zipping /unzipping, etc
Is there a way to add the proper code to create the content for the body at step "oEmailItem.BodyFormat = olFormatHTML"?
What would be the proper statement to add to the VBS to loop through a list if hyperlinks and write them to the email body?
Currently the first script i use echoes the variables to the .rtf file. I tried loading the contents of the entire .rtf file to the email body but it loses all the layout and everything runs together.
Thanks for any ideas. Note that i the code below i used "examples" for the recipient but the rest is pretty much working as is.
Option Explicit
Dim objArgs, OutApp, oNameSpace, oInbox, oEmailItem, olMailItem
Dim a, oAttachments, subjectStr, olFormatHTML
olMailItem = 0
olFormatHTML = 2
Set objArgs = WScript.Arguments 'gets paths of selected files
Set OutApp = CreateObject("Outlook.Application") 'opens Outlook
Set oEmailItem = OutApp.CreateItem(olMailItem) 'opens new email
oEmailItem.To = "recipient@email.com"
oEmailItem.cc = ""
For a = 0 to objArgs.Count - 1
Set oAttachments = oEmailItem.Attachments.Add(objArgs(a))
subjectStr = subjectStr & Right(objArgs(a),Len(objArgs(a))-(InStrRev(objArgs(a),"\"))) & ", " 'recreates the default Subject e.g. Emailing: file1.doc, file2.xls
Next
If subjectStr = "" then subjectStr = "No Subject "
oEmailItem.Subject = "Subject"
oEmailItem.BodyFormat = olFormatHTML
oEmailItem.Display
oEmailItem.Send
Dim objArgs, OutApp, oNameSpace, oInbox, oEmailItem, olMailItem
Dim a, oAttachments, subjectStr, olFormatHTML
olMailItem = 0
olFormatHTML = 2
Set objArgs = WScript.Arguments 'gets paths of selected files
Set OutApp = CreateObject("Outlook.Application") 'opens Outlook
Set oEmailItem = OutApp.CreateItem(olMailItem) 'opens new email
oEmailItem.To = "recipient@email.com"
oEmailItem.cc = ""
For a = 0 to objArgs.Count - 1
Set oAttachments = oEmailItem.Attachments.Add(objArgs(a))
subjectStr = subjectStr & Right(objArgs(a),Len(objArgs(a))-(InStrRev(objArgs(a),"\"))) & ", " 'recreates the default Subject e.g. Emailing: file1.doc, file2.xls
Next
If subjectStr = "" then subjectStr = "No Subject "
oEmailItem.Subject = "Subject"
oEmailItem.BodyFormat = olFormatHTML
oEmailItem.Display
oEmailItem.Send