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?
Assuming you can access the feature, it is fairly simple to have a batch file login to a database and send mail. A batch file can easily run a VBScript via CSCRIPT. A quick google search finds many links showing how to send email with VBScript.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
Batch file processing ends when execution reaches the end of the batch file. The trick therefore is to use the goto command to jump to a label right before the end of the file, so that execution “falls off the end”.
Blat:
blat -to [email protected] -server smtp.example.com -f [email protected] -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 = "[email protected]"
$objMailMessage.To.Add("[email protected]")
$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 [email protected] ^
-From [email protected] ^
-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 ^
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With