How can I send an email from Mac Terminal using a bash shell script?

How can I send an email from my Mac Terminal using a bash shell script?


I have tried the following shell script:


send_email.sh


but I did not receive anything in my Mail app Inbox.


#!/bin/bash
recipient="b*****7@**mail.com"
subject="Testing..."
body="This is the body of the email"
echo "$body" | mail -s "$subject" "$recipient"

I typed this at the bash prompt:


bash send_email.sh


I have an Apple Silicon MacBook Pro with Sequoia 15.5.


I have searched on Google for any help but I cannot get any further forward.


Can anyone help?


[Edited by Moderator]



MacBook Pro 16″, macOS 15.5

Posted on Jul 6, 2025 08:40 AM

Reply
8 replies

Jul 6, 2025 09:59 AM in response to sjlearmonth

sjlearmonth wrote:

How do I get a mail server running locally?


I had suggested the Python solution because it’s contained, maintainable, and won’t get you into a quagmire that can be maintaining and updating and securing and troubleshooting a local mail server.


The following might get you to a working local mail server configuration, depending on your ISP:

https://altoplace.com/macos/create-a-send-only-smtp-server-on-macos/


I do not recommend running a local mail server. Not unless you want to have a hobby of running a mail server.

Jul 6, 2025 09:26 AM in response to sjlearmonth

The mail command assumes a mail server is running locally, and you probably don’t have and don’t want that here.


The semi-recent Python answer linked below should get you closer. Note the other (and older) mail commands shown, and particularly those using telnet and port 25 in that same thread will not work in 2025, or will require getting a local mail server at least partially configured and running.


https://stackoverflow.com/a/1940937


The Python example avoids the need to set up a local mail server, and all that can entail. You will need to download and install Python from the Python website.


Any examples you find referencing TCP port 25 (and not 587, or maybe 465) is older info, and is likely going to be trouble.


Older versions of macOS Server and of its predecessor OS X Server all had capable mail server and particularly had related management tools integrated, but that all ended with the release of macOS “Server” 10.14 and later.

Jul 6, 2025 12:16 PM in response to sjlearmonth

sjlearmonth wrote:

Please provide a series of steps which is a solution to the original question using ONLY bash.

Thank you.


Connect into your mail provider’s SMTP server using TCP port 465 or 587 as appropriate likely using TLS, then send over the appropriate SMTP commands including authentication, and then send over the message data.


This might get you going, with bash and OpenSSL: https://stackoverflow.com/a/75763179


Python, Perl, and some other environments have this mail server connection support available. The old version of bash on macOS AFAIK does not. The newer zsh is better here, but that and AppleScript scripting the Mail app are also not the requested bash.

Jul 6, 2025 01:27 PM in response to sjlearmonth

sjlearmonth wrote:

Please provide bash script commands.


If you are unfamiliar with the linked bash shell script, and a script which necessarily uses OpenSSL for the connection security required by most modern mail servers, I’d suggest acquiring some formal assistance.


This given the provided bash and Python code, and the Postfix info, all seem inadequate for your needs.


Of all this, I’d again suggest using the previously-linked Python code.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I send an email from Mac Terminal using a bash shell script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.