Automate Outlook Email Creation on macOS with Body Text and Attachments from Terminal
If you often work in the macOS Terminal and want to quickly draft an Outlook email — complete with body text and file attachments — this…
If you often work in the macOS Terminal and want to quickly draft an Outlook email — complete with body text and file attachments — this guide is for you.
Below, you’ll learn how to create a smart Bash script that:
✅ Uses standard input (stdin) as the email body,
✅ Accepts any number of attachments as parameters,
✅ Converts all file paths to absolute paths, so you don’t have to worry about where you run it from.
💡 Why This Is Useful
This script is perfect for:
- Automating reports
- Sharing logs or exports
- Combining scripts with email delivery
You can pipe output from any command directly into your email body and attach related files — all in one go.
The Script
Create a file called outlook.sh:
#!/usr/bin/env bash
# Read stdin into bodyText (if any)
if [ -t 0 ]; then
bodyText=""
else
bodyText=$(cat)
fi
# Validate attachments and build AppleScript commands
attachmentScript=""
for file in "$@"; do
# Convert to absolute path
absPath="$(cd "$(dirname "$file")"; pwd)/$(basename "$file")"
if [[ ! -f "$absPath" ]]; then
echo "Error: File not found: $file"
exit 1
fi
attachmentScript+="
make new attachment at newMessage with properties {file:(POSIX file \"$absPath\")}"
done
# Build and run AppleScript
osascript <<END
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:"Report"}
$(if [[ -n "$bodyText" ]]; then echo "set content of newMessage to \"$(printf "%s" "$bodyText" | sed 's/"/\\"/g')\""; fi)
$attachmentScript
open newMessage
activate
end tell
END🛠️ Making It Executable
Make the script executable:
chmod +x outlook.sh⚙️ How to Use It
1️⃣ Just Body Text
Send content to Outlook via a pipe:
echo "Hello, this is the body of the email." | ./outlook.shResult:
A new email draft with the text as the body and no attachments.
2️⃣ Just Attachments
Attach one or more files:
./outlook.sh ./report.pdf ./summary.txtResult:
A new email draft with the files attached and an empty body.
3️⃣ Body Text + Attachments
Combine both:
echo "Here is the report you requested." | ./outlook.sh ./report.pdf ./data.csvResult:
A new email draft with the text as the body and the files attached.
The script automatically converts all relative paths to absolute paths, so:
./outlook.sh ./file.txtand
./outlook.sh /Users/you/Documents/file.txtboth work identically.
🧠 How It Works
- Stdin check:
if [ -t 0 ]; thendetects whether stdin is empty. - Body text capture:
If you pipe text, it’s read intobodyText. - Path conversion:
Each file path is turned into an absolute path with:
absPath="$(cd "$(dirname "$file")"; pwd)/$(basename "$file")"- AppleScript integration:
AppleScript builds the email draft in Microsoft Outlook, sets the content, and attaches the files.
✅ Example Workflow
Here’s a real example that produces a professional email draft in seconds:
echo "Dear team,
Attached are this week's reports.
Best regards,
Kostas
" | ./outlook.sh ./weekly_report.pdf ./metrics.xlsx🎯 Conclusion
With this simple script, you can seamlessly bridge the gap between your macOS Terminal workflows and Microsoft Outlook. Whether you need to quickly draft emails with dynamic content, attach multiple reports, or automate recurring communications, this approach saves time and reduces repetitive clicks.
Feel free to customize the script further — adding recipients, setting custom subjects, or integrating it into larger automation pipelines. With just a few lines of Bash and AppleScript, you unlock a powerful productivity boost right from your command line.