Automatic emails from command line in Windows
Did you ever want to send automatic emails, whether the sending was triggered by an event or just wanted to send the email at a certain time of the day or month?
I am not talking about Email Marketing Software, although that sounds interesting too. I am talking about sending emails from a batch file. You make a backup of a file and need to know when the task was finished or you want to send some files to a group of users, let’s say some weekly reports, logs, the contents of an html form, etc… How do you do that? Here is a real example.
Many people look for an outlook command line to send email, however that is less powerful than my method. At the end of the article you can see a limited way to do that. Before looking at the Outlook solution though, check my solution to send email from command line. It’s powerful, elegant and fast.
How did I Start to Send Emails from Command Line?
I was recently asked to find a solution for sending some huge Excel reports to a list of users. Previously, the application was automatically producing and then sending the reports using Outlook. The application didn’t compress the files and that worked fine until a huge report was requested by the Management. The huge report couldn’t be sent anymore by email because the servers will deny big attachments.
Using third party automatic email attachment compression software, (like Winzip Courier), didn’t work because works with an Outlook message, but this wasn’t the case.
We then decided to let the application do only the report generation and we were going to compress and email the files using other software.
First of all there are several applications of the type “Automatic Email Senders“. The “Automatic Email Senders” software applications are very complex, take space on the hard-drive and cost money. Our solution is a free solution for the regular small business network administrator, who doesn’t always have the money to spend.
The Technical Details to Send Emails from Command Line?
Our solution used 7zip as a compression utility and Blat as the emailing utility.
7zip is an open-source file archiver. It has a graphical interface but we only used the command line. You can download 7zip here: http://www.7-zip.org/.
Blat is a free, small application, without a graphical interface, that can send email message from the command line. You can download Blat here: http://www.blat.net/.
Following is a batch file example to help you start your own.
:: courtesy of http://dorianblog.info :: The paths to the report files and the Output folder for the archived file. Set InputPath=\\192.168.168.168\shared\Reports Set OutputPath=\\192.168.168.168\shared\Reports\Output :: Insert the date into a variable to use it as a file name FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set date=%%a%%b%%c) Set Filetosend=Real-file-Name :: The recipient list Set RecipientListfirstname.lastname@example.org,email@example.com,firstname.lastname@example.org cd C:\Base-Folder\ :: Delete any previously created file del /q /f "C:\Base-Folder\%Filetosend%.zip" :: We check if we are connected to the share. ctrl.txt is a dummy file created only for this. :: It is wise to use a specially created user that only has access to the share. The password of this user is stored on this batch file. :: We need this in order to make sure the scheduled task runs successfully even if we are not logged in. if exist "%InputPath%\ctrl.txt" goto auto net use "%InputPath%" /user:shareUser share-password :auto :: First compress the file to send using maximum compression. -mx can be lower for reducing the compression time. C:\Base-Folder\7za a -y -tzip -mx=9 -r %OutputPath%\%date%-%Filetosend%.zip %InputPath%\%Filetosend%.xls :: If you have winzip installed and you have the command line you can use this. "C:\Program Files\WinZip\WZZIP.EXE" -a "%OutputPath%\%date%-%Filetosend%.zip" "%InputPath%\%Filetosend%.xls" :: We send the file now using blat. We use the submit port on our SMTP server and authentication. :: The authenticated user is "emailuseraccount" with the password "email-password". :: The variable %RecipientList% contains all of your recipients separated by comas. blat -server "mail.some-domain.com" -port 587 -u emailUseraccount -pw email-password -f email@example.com -noh -to %RecipientList% -subject "Reports %Filetosend%" -body "Attached you will find your Jet Report" -attach "%OutputPath%\%date%-%Filetosend%.zip" :: We disconnect the network net use /delete %InputPath% :: Remove all the variables Set Base-Folder= Set InputPath= Set OutputPath= Set RecipientList=
If you found this article useful, post a link to this page or make a comment. You can also comment with improvements of the batch.
If you are looking at a way to automatically send emails through Outlook, there is a limited function of this. The function is limited to composing emails only and they cannot be automatically sent using command line switches. The following command:
“C:\Program Files\Microsoft Office\Office11\Outlook.exe” /m “firstname.lastname@example.org” /a “c:\Somefile.zip”
will create a message addressed to email@example.com with the somefile.zip as an attachment. The message will have to be manually sent by an operator. An alternative for a fully automated solution is to use a key stroke simulator such as SendKeys to emulate CTRL+S, (send). Another solution to automatically send emails from Outlook is to use TASK SCHEDULER to call a VBS script. The VBS script does the sending through Outlook. There are a few ways to do this using the Outlook Object Model, or MAPI Mail, or CDO mail. But this is out of the scope of this article and I see very little advantage in developing a VBS script for Outlook.