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:

; you must change to your smtp server,

; smtp port (normally 25)

; error log

; Gmail username and password

; the “From: ” header of the message content

; the “To: ” header of the message content

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

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



2 thoughts on “Batch Script: Send Email

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s