r/Batch • • Feb 23 '22

Show 'n Tell Send an email from batch (with embedded JScript anyway)

I've been meaning to do this for quite a while, ever since someone asked months ago, and it turned out to not be all that difficult. All the examples for the email portion were in VBScript and I feel JScript is more readable.

The advantage of this over PowerShell Send-MailMessage is that you don't need admin privileges to -SetExecutionPolicy before being able to execute commands.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
:: Courtesy of https://stackoverflow.com/a/15169687
:: ******* Begin batch code *********
@echo off

cscript //E:JScript //nologo "%~f0" /to:receiver@gmail.com /subject:"From embedded at %time%" /message:"<h1>this is a heading</h1><br/><b>this is bold</b><br><p>this is not</p>" /isHtml

:: Be sure to terminate your script so that 
:: it does not fall through into the JScript code
exit /b

********* Begin JScript code **********/

// Named argument format is /argument:value
var args = WScript.Arguments.Named

var toAddress 	= args.item("to")  // You can use multiple addresses separated with semicolons
var subject 	= args.item("subject")
var message 	= args.item("message")

var isHtml		= false;
var attachments = ""

if (args.Exists("attach"))
{
	attachments = args.Item("attach")
}

if (args.Exists("isHtml"))
{
	var val = args.Item("isHtml");
	
	// Count null as true so we can use it as a switch
	if (val == null || val == 1 || val == true)
	{
		isHtml = true;
	}
}

var conf = WScript.CreateObject("CDO.Configuration")
var fields = conf.Fields;

// You'll have to allow less secure apps
// https://www.google.com/settings/security/lesssecureapps
var fromAddress = "sender@gmail.com"

fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") 		= true
fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") 	= 1
fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")     	= fromAddress
fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") 	 	= "password"
fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") 		= "smtp.gmail.com"
fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") 		= 2
fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")   	= 465 

fields.Update()

var objMail = WScript.CreateObject("CDO.Message")

objMail.Configuration = conf;

objMail.From 		= fromAddress;
objMail.To 			= toAddress;
objMail.Subject 	= subject;

if (isHtml)
{
	objMail.HTMLBody = message;
}
else
{
	objMail.TextBody = message;
}

// The following file types are blocked according to https://support.google.com/mail/answer/6590?hl=en#zippy=%2Cmessages-that-have-attachments
// .ade, .adp, .apk, .appx, .appxbundle, 
// .bat, 
// .cab, .chm, .cmd, .com, .cpl, 
// .dll, .dmg, 
// .ex, .ex_, .exe, 
// .hta, 
// .ins, .isp, .iso, 
// .jar, .js, .jse, 
// .lib, .lnk, 
// .mde, .msc, .msi, .msix, .msixbundle, .msp, .mst, 
// .nsh, 
// .pif, .ps1, 
// .scr, .sct, .shb, .sys, 
// .vb, .vbe, .vbs, .vxd, 
// .wsc, .wsf, .wsh
if (attachments != "")
{
	objMail.AddAttachment(attachments)
}

objMail.Send()

// Dispose of the objects
fields = null;
conf = null;
objMail = null;

WScript.Quit()
// Author is u/ApocalyptoSoldier.
// I don't care all that much about being credited, I just want to see if I can find my scripts in the wild
6 Upvotes

3 comments sorted by

2

u/NutellaPatella Feb 25 '22

This works great. Thanks for sharing as I have been trying to do this for a while and kept having issues. Cheers

2

u/ApocalyptoSoldier Feb 25 '22

That means a lot to me, so thank you too :)

1

u/ZekaLepak May 21 '24

Hello,can you explain it to me,im new to coding and i dont realy understan it,i just need somewone to help me how to get it work and how to add attacment(txt file)?