Batch Script: Send Email


sendmail is a handy tool that can be used in batch script to send email.

1) Configure your SMTP settings in the sendmail.ini file. Following is a sample to use Gmail:

[sendmail]
; you must change mail.mydomain.com to your smtp server,
smtp_server=smtp.gmail.com

; smtp port (normally 25)
smtp_port=465

; error log
error_logfile=mail_error_log.txt

; Gmail username and password
auth_username=xxxxxx@gmail.com
auth_password=***********

; the “From: ” header of the message content
force_sender=xxxxxx@gmail.com

; the “To: ” header of the message content
force_recipient=receipient@anydomain.com

2) Add the following code into the batch script to send email when the former command failed.

REM Send email if the previous command failed

echo ‘Job completed successfully’ > success_mail.txt
echo ‘Job failed’ > error_mail.txt

if errorlevel 1 goto error

echo Success
sendmail.exe -t < success_mail.txt
goto end

:error
echo Error!
sendmail.exe -t < error_mail.txt

:end

Advertisements

2 thoughts on “Batch Script: Send Email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s