topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday December 12, 2024, 1:59 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Best Script type to build an email body line by line  (Read 4241 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
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.


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


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Best Script type to build an email body line by line
« Reply #1 on: March 10, 2017, 04:27 AM »
What's the point in creating the RTF file in the first place (and why RTF for a list of links, plain text is easier)?
Where do the list of links that go into the RTF come from?

Sounds like a lot of needless double handling at the moment.

You could just create a HTML file with the links and use it as the body of the email like back here.

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,939
    • View Profile
    • Donate to Member
Re: Best Script type to build an email body line by line
« Reply #2 on: March 10, 2017, 09:07 AM »
Just make sure that when you decide to drop the whole RTF thing and make an HTML-based mail body, you keep the HTML layout as simple as possible. Forgo the use of CSS as much as you can, because if you think browsers on computers, tablets and/or smartphones can suck, you haven't had the "pleasure" of making HTML content look the same in mail clients.

Better use simple HTML v4.0 stuff.  The above isn't valid if you just create a file only with a set of links...but once that list gets spread around, there will soon be someone telling you to make something look visually pleasing, add the company logo, etc., etc. You'd think you had a headache till now?

I need to transport builds of from different versions of our software and was asked to make a dynamically generated mail message with logo, layout and all...in batch and send it (to be in line with the rest of the procedure). Serious levels of frustration getting things to look the same on multiple mail clients, browsers and devices, I now get completely why people use mail-list companies and their software to send visually pleasing mail messages.

For the sending part, I tried Blat, but had more success with CMail. Ah well, both are freeware.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Best Script type to build an email body line by line
« Reply #3 on: March 10, 2017, 05:46 PM »
thanks all.  :-[
I gotta remember to stop making posts a 2 am.  :huh:
After giving it more thought in the light of day, i realized that I was making life hard for myself.  The reason for the rtf is that the written text can become active URL's once they are selected and clicked whereas in a text file this does not happen.
if i could have "read" the contents of the file whether rtf or txt into the body of the email line by line so it would retain the proper formatting, this would have solved the problem as the email body is html.

However:  After giving it a lot of thought, I decided that the person i was doing this for was going to have to give up and just open the attached file itself and from there either copy the link to a browser or do with it as they may need to get where they want to go.

But.. at 2am .. sometimes things don't look the same.
I am also currently caring for two very ill Flu victims :( (wife and daughter)

Either way, i have dropped the idea of making life easier for this person.
 
My only glitch in the works now is that there is almost NO way to get Outlook to SEND the blasted email once composed except by use of the vbs script here which is one I configured a year or so ago.   It was designed to be a 'drag and drop' email app which it works great for.  You see a brief BLINK and the email is gone.

Unfortunately.  I need to tie all this to the nightly backup which is a batch script.  I can use 'ipm.note' to build the email in batch and it works great!
BUT it wont SEND it.
I can build the file in the batch then use the VBS to send it ..
But
so far i have been unable to reconfigure the vbs to use a specific file rather than working as designed to capture the files dropped onto the VBS script.
This is from a lack of knowing much about WSH i am sure.  The original VBS file was one I reconfigure for my needs at  the time.  And it still works perfect for what i needed as i use it every day to drag and drop all kinds text files or zip files etc that i need to email to myself.

If you happen to see how i can set it to send a specific file at "C:\lists\listname.rtf"  as the only attachment?  That would do the trick.

Once the backup runs, i can call that vbs to send the resulting list.rtf to the person that needs it.  and it would all happen "auto-magically" at 4 am every night when the backup ran without me doing anything.
So i can go back to taking care of the ill. :(

PS:  4WD  I think what I was doing back then was sending an image file?  And i think we did end up with a BLAT script to do it? I wil revisit that as soon as i get a few minutes to do so.  But if BLAT was how it worked it may not "fly" now with the new exchange server setup and al the security checks on email configurations.
But it might work.   And i will look at that option.
I just know for SURE if i can get the VBS to handle a single named file at a specific location that it will for 100% sure work.









 



« Last Edit: March 10, 2017, 05:53 PM by questorfla »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: Best Script type to build an email body line by line
« Reply #4 on: March 10, 2017, 10:03 PM »
My only glitch in the works now is that there is almost NO way to get Outlook to SEND the blasted email once composed except by use of the vbs script here which is one I configured a year or so ago.   It was designed to be a 'drag and drop' email app which it works great for.  You see a brief BLINK and the email is gone.

...

so far i have been unable to reconfigure the vbs to use a specific file rather than working as designed to capture the files dropped onto the VBS script.

Powershell

Code: Text [Select]
  1. %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -sta -noprofile -executionpolicy bypass -Window Hidden -File "EmailArc.ps1" [email protected] somefile.rtf


I think what I was doing back then was sending an image file?

Yes but you might find that if you just write out a file with the links and call it links.html and then tell blat/cmail to use it as a HTML email body that it might be enough so that it gets interpreted as an email full of clickable links at the other end.

Otherwise, it's no big deal to write each link as <a href=http://some.link1.com>Link1</a> in a file and use that as the HTML body.
« Last Edit: March 10, 2017, 10:37 PM by 4wd »

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Re: Best Script type to build an email body line by line
« Reply #5 on: March 22, 2017, 04:58 PM »
i almost forgot i had left this 'hanging'. :huh:
That's what happens when everyone 'else' gets the Flu bug.

Anyway, I took lazy way out. 

I set the working vbs file to run with task-scheduler after the nightly backup and added the creation of the list to the backup routine.
I did try including the VBS as an integral part of the backup.bat file using : "cscript //nologo emailit.vbs" as the final step.   But this still gets skipped with no obvious reasons or errors.  As long as I eave Outlook open, running the VBS as a stand-alone item with the scheduler works like a charm so.. End of that issue.

I am still looking at your setup, 4WD, but for I must be doing something wrong or trying to hard on making the text become an active URL.  Or maybe it is that I really need to stick with Outlook where possible because of all the automatic sync on all devices in Exchange Server.   I did get the latest BLAT though and will keep trying.  The end results are an attached RTF file which is good enough for now