We have a script to backup files. After the backup operation is over, we would like to send a report as an email notification to some of our email addresses.
How could this be done?
We have a script to backup files. After the backup operation is over, we would like to send a report as an email notification to some of our email addresses.
How could this be done?
Blat:
blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
You can also use a Power Shell script:
$smtp = new-object Net.Mail.SmtpClient("mail.example.com")
if( $Env:SmtpUseCredentials -eq "true" ) {
$credentials = new-object Net.NetworkCredential("username","password")
$smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "script@mycompany.com"
$objMailMessage.To.Add("you@yourcompany.com")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"
$smtp.send($objMailMessage)
PowerShell comes with a built in command for it. So running directly from a .bat
file:
powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
-SmtpServer server.address.name ^
-To someone@what.ever ^
-From noreply@possibly.fake ^
-Subject Testing ^
-Body 123
NB -ExecutionPolicy ByPass
is only needed if you haven't set up permissions for running PS from CMD
Also for those looking to call it from within powershell, drop everything before -Command
[inclusive], and `
will be your escape character (not ^
)
bmail. Just install the EXE and run a line like this:
bmail -s myMailServer -f Sender@foo.com -t receiver@foo.com -a "Production Release Performed"
Easiest way is to use a third-party application as mentioned by others
If that is not an option I wrote a simple sendmail utility using vbscript & CDO which I called from a batch script
See the examples here http://www.paulsadowski.com/WSH/cdo.htm
We use blat to do this all the time in our environment. I use it as well to connect to Gmail with Stunnel. Here's the params to send a file
blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body" -attach c:\temp\file.txt
Or you can put that file in as the body
blat c:\temp\file.txt -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject"
You can also use sendmail. I am using it in this subversion hook to send email notifications: post-commit hook
There are multiple methods for handling this problem.
My advice is to use the powerful Windows freeware console application SendEmail.
sendEmail.exe -f sender.from@mail.com -o message-file=body.txt -u subject message -t to.email.address@mail.com -a attachment.zip -s smtp.gmail.com:446 -xu gmail.login -xp gmail.password