Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending mail from Batch file [closed]

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?

like image 856
user73628 Avatar asked Apr 02 '09 13:04

user73628


People also ask

Can you send an email from a batch file?

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.

Why is %% used in batch file?

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.

How do I close a batch file without closing the window?

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”.


3 Answers

Blat:

blat -to [email protected] -server smtp.example.com -f [email protected] -subject "subject" -body "body" 
like image 99
Colin Pickard Avatar answered Oct 16 '22 22:10

Colin Pickard


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)
like image 45
Philibert Perusse Avatar answered Oct 16 '22 21:10

Philibert Perusse


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 ^)

like image 36
Hashbrown Avatar answered Oct 16 '22 22:10

Hashbrown