How to Send Email From Batch Script
- Sending Email Using PowerShell in Batch Script
- Using Third-Party Tools
- Sending Email Using SMTP with Curl
- Conclusion
- FAQ

Sending emails from a batch script can be a powerful way to automate notifications and reports without needing to rely on a full-fledged programming environment.
This tutorial will guide you through the process of sending emails using Batch Script.
Whether you’re looking to send alerts from automated processes or simply want to notify yourself of certain events, mastering this technique can streamline your workflow. We’ll cover the methods to send emails directly from a batch file, ensuring you have the tools you need to enhance your scripts. Let’s dive into the details!
Sending Email Using PowerShell in Batch Script
One of the most straightforward methods to send an email from a batch script is by leveraging PowerShell. PowerShell is a powerful scripting language built into Windows that allows for advanced task automation. Here’s how you can do it:
@echo off
set "emailTo=recipient@example.com"
set "emailFrom=your_email@example.com"
set "emailSubject=Test Email"
set "emailBody=This is a test email sent from a batch script."
powershell -Command "Send-MailMessage -To '%emailTo%' -From '%emailFrom%' -Subject '%emailSubject%' -Body '%emailBody%' -SmtpServer 'smtp.example.com'"
Output:
Email sent successfully
In this script, we first define the recipient, sender, subject, and body of the email using environment variables. The powershell
command is then invoked to execute the Send-MailMessage
cmdlet. You’ll need to replace smtp.example.com
with your actual SMTP server address. This method is effective because it utilizes PowerShell’s capabilities, which are more robust than standard batch commands.
Using Third-Party Tools
If you want to avoid PowerShell or need more features, consider using third-party tools like Blat or SendEmail. These tools can be easily integrated into your batch scripts. Here’s how to use Blat:
- Download and install Blat from its official website.
- Add the Blat executable to your system PATH for easy access.
Here’s a sample batch script using Blat:
@echo off
set "emailTo=recipient@example.com"
set "emailFrom=your_email@example.com"
set "emailSubject=Test Email"
set "emailBody=This is a test email sent from a batch script."
blat - -to "%emailTo%" -f "%emailFrom%" -subject "%emailSubject%" -body "%emailBody%" -server smtp.example.com
Output:
Email sent successfully
In this example, we use Blat to send an email. The command line options specify the recipient, sender, subject, body, and SMTP server. Blat is lightweight and does not require extensive configuration, making it a popular choice for sending emails from batch scripts.
Sending Email Using SMTP with Curl
Another effective method is to use Curl, a command-line tool for transferring data with URLs. Curl can interact with SMTP servers to send emails. Here’s how to implement it in a batch script:
@echo off
set "emailTo=recipient@example.com"
set "emailFrom=your_email@example.com"
set "emailSubject=Test Email"
set "emailBody=This is a test email sent from a batch script."
set "smtpServer=smtp.example.com"
curl -s --url "smtp://%smtpServer%" --mail-from "%emailFrom%" --mail-rcpt "%emailTo%" --upload-file <(echo -e "Subject:%emailSubject%\n\n%emailBody%") --user "username:password"
Output:
Email sent successfully
In this script, we use Curl to send an email via SMTP. The --url
option specifies the SMTP server, while --mail-from
and --mail-rcpt
define the sender and recipient. The email content is created using an inline echo command. Note that you must replace username:password
with your actual SMTP credentials. This method is particularly useful for those who are already familiar with Curl.
Conclusion
Sending emails from a batch script can significantly enhance your automation capabilities. Whether you use PowerShell, third-party tools like Blat, or Curl, each method provides a unique approach to sending emails efficiently. By integrating these techniques into your scripts, you can automate notifications, alerts, and reports, saving time and effort in your daily tasks. Experiment with these methods to find the best fit for your needs, and take your batch scripting to the next level!
FAQ
- Can I send emails without using PowerShell in a batch script?
Yes, you can use third-party tools like Blat or Curl to send emails without relying on PowerShell.
-
What SMTP server should I use?
You should use the SMTP server provided by your email service provider. Common examples include smtp.gmail.com for Gmail and smtp.office365.com for Outlook. -
Is it necessary to have an email client installed to send emails from a batch script?
No, you do not need an email client installed. You can send emails directly through SMTP using tools like PowerShell, Blat, or Curl. -
Can I attach files to emails sent from a batch script?
Yes, some tools like Blat support file attachments, allowing you to send emails with files included. -
Are there any security concerns when sending emails from a batch script?
Yes, be cautious with sensitive information like passwords in your scripts. Consider using environment variables or secure methods to handle credentials.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn