Git Bash执行邮件脚本报错'mail: command not found',求HTML邮件发送方案
Hey there! That "mail: command not found" error pops up because Git Bash doesn’t include the standard Unix mail tool out of the box. But don’t worry—we’ve got two solid ways to get your HTML emails sent from Git Bash:
Option 1: Use Windows PowerShell (No Extra Installs Needed)
Git Bash can directly call Windows tools, and PowerShell has a built-in Send-MailMessage cmdlet that handles HTML emails perfectly. Here’s a ready-to-use command you can tweak:
powershell.exe -Command "Send-MailMessage -From 'your-email@example.com' -To 'dineshl@gmail.com' -Subject 'This is the subject' -Body '<h1>Your HTML Message Goes Here</h1><p>With paragraphs and other tags!</p>' -BodyAsHtml -SmtpServer 'smtp.example.com' -Port 587 -UseSsl -Credential (Get-Credential)"
A few key notes to make this work:
- Replace
smtp.example.comwith your email provider’s SMTP server (e.g.,smtp.gmail.comfor Gmail). - If you’re using Gmail with 2FA enabled, you’ll need to use an App Password instead of your regular account password.
- The
-Credential (Get-Credential)part will prompt you for your email password—avoid hardcoding plaintext passwords in scripts; use secure credential storage for automation.
Option 2: Install the mail Utility via Pacman
Git Bash is built on MSYS2, so you can use the pacman package manager to install the missing mail tool:
First, update the package list in Git Bash:
pacman -SyInstall the
mailutilspackage (which includes themailcommand):pacman -S mailutilsConfigure your SMTP settings by editing the
/etc/mail.rcfile. Open it withnano:nano /etc/mail.rcAdd these lines (replace placeholders with your email details):
set smtp=smtp://smtp.example.com:587 set smtp-auth=login set smtp-auth-user=your-email@example.com set smtp-auth-password=your-app-password-or-password set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/Save and exit (in nano, press
Ctrl+O, hitEnter, thenCtrl+X).Now you can use a modified version of your original script to send HTML emails:
echo "<h1>HTML Message goes here</h1>" | mail -s "$(echo -e 'This is the subject\nContent-Type: text/html')" dineshl@gmail.com
Critical Security Reminder
Never hardcode your email password directly in scripts or configuration files. For automated workflows, use environment variables or encrypted credential storage instead.
内容的提问来源于stack exchange,提问作者Dinesh

