How to Start a New Email with a Specific Recipient in Batch Script
A useful feature for many scripts, especially those that generate reports or require user feedback, is the ability to open a new email draft, pre-filled with a recipient, subject, and body text. While batch scripting has no native command to send an email, it can easily trigger the user's default email client (like Outlook, Thunderbird, or the Windows Mail app) to open a new, pre-populated message.
This guide will teach you how to use the standard mailto: protocol in combination with the built-in START command. You will learn how to specify not just the recipient, but also the CC, BCC, subject, and a multi-line body, and how to correctly handle special characters to make your command robust.
The Core Concept: The mailto: Protocol
The mailto: protocol is a standard internet scheme for creating hyperlinks that open a new email draft. It's the same technology that works when you click an email address on a website.
The basic syntax is:
mailto:recipient@example.com
You can add more fields by starting with a ? for the first field, and then separating subsequent fields with an ampersand (&).
The Core Command: START
The START command is the Windows utility for launching programs and opening files or URLs with their default handlers. When START is given a string that begins with a protocol like mailto:, it hands it off to the Windows Shell, which then finds and opens the user's default email client.
Basic Example: A Simple Email to One Recipient
This script will open the user's default email client with a new message addressed to support@example.com.
@ECHO OFF
ECHO --- Opening a new email draft ---
ECHO.
ECHO This will open your default email client.
PAUSE
START "mailto:support@example.com"
Adding More Details (Subject, Body, CC, BCC)
You can build a much more detailed email draft by adding parameters to the mailto: link.
?subject=Your Subject Here&cc=someone_else@example.com&bcc=hidden_copy@example.com&body=Your message body here.
Script with All Options
@ECHO OFF
START "mailto:recipient@example.com?cc=manager@example.com&subject=Project Status Report&body=Here is the report you requested."
When this runs, it will open a new email draft with the "To", "CC", "Subject", and "Body" fields already filled in.
How the Command Works (Protocol Handlers)
The START command itself doesn't know what an email is. It simply sees the mailto: protocol at the beginning of the string. It passes this string to the Windows Shell, which looks in the registry to find the default application registered to handle the mailto: protocol. It then launches that application (e.g., OUTLOOK.EXE) and passes the full string to it as a command-line argument. The email client is responsible for parsing the string and populating the new email draft.
Common Pitfalls and How to Solve Them
CRITICAL: The URL Contains Special Characters (like &)
This is the most important pitfall. The ampersand (&) is a special character in batch that is used to separate multiple commands on a single line. If your mailto: link contains a & (which it will if you have more than one field), the batch interpreter will break the command.
Example of script with error:
REM This will FAIL.
START mailto:user@example.com?subject=Hello&body=Hi
The interpreter sees &body=Hi as a separate, invalid command.
Solution: Always Enclose the Entire Link in Double Quotes
By wrapping the entire mailto: string in double quotes, you tell the batch interpreter to treat it as a single, literal argument to be passed to the START command.
REM This is the correct, safe syntax.
START "" "mailto:user@example.com?subject=Hello&body=Hi"
The "" is a best practice "dummy title" for the START command.
Problem: Handling Spaces and Newlines in the Body
A proper URL should not contain literal spaces or newline characters. They must be percent-encoded.
- Space should be replaced with
%20. - Newline should be replaced with
%0A.
For example: here a script to Build a Multi-line Body
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "body=Hello,%0APlease find the attached report.%0A%0AThank you,"
SET "body=!body: =%%20!"
START "" "mailto:manager@example.com?subject=Report&body=!body!"
ENDLOCAL
Practical Example: A "Send Log File" Helper Script
This script prepares an email for the user to send to IT support. It includes a pre-filled subject and a body that instructs the user to attach a specific log file.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "Recipient=it.support@mycorp.com"
SET "Subject=Application Error Report"
SET "LogFile=%TEMP%\app_error.log"
ECHO --- Create Support Email ---
ECHO This will open an email draft to send to IT.
ECHO Please attach the following file: %LogFile%
ECHO.
PAUSE
REM --- Build the multi-line, URL-encoded body ---
SET "line1=Hello IT Support,"
SET "line2=I encountered an error while using the application."
SET "line3=Please see the attached log file for details."
SET "line4="
SET "line5=Thank you,"
SET "line6=%USERNAME%"
SET "body=!line1!%0A!line2!%0A!line3!%0A!line4!%0A!line5!%0A!line6!"
SET "body=!body: =%%20!"
SET "Subject=!Subject: =%%20!"
REM --- Construct and launch the final mailto link ---
START "" "mailto:%Recipient%?subject=!Subject!&body=!body!"
ENDLOCAL
Conclusion
The START command combined with the mailto: protocol is the definitive, built-in method for opening a pre-filled email draft from a batch script.
Key takeaways for success:
- This method opens a draft for the user to send; it does not send the email automatically.
- Always enclose the entire
mailto:link in double quotes to handle the&character correctly. - It's a best practice to use the
START "" "..."syntax. - For bodies with spaces or newlines, you must percent-encode them (
%20for space,%0Afor newline) for maximum reliability.