12

I'm trying to send an email from the command line but it does not work:

echo "Hello World" | mail -s "Test email" [email protected]

Years ago "it just worked" because mail would (apparently) use an smtp server from Mail.app and send the email. There are two working smtp servers in Mail.app (iCloud and my ISP on port 25) but still mail cannot send email from the command line.

What's going on?

2 Answers 2

7

OK, it works now. I'll leave this report for posterity.

When using mail from the commandline, postfix has to be configured properly. It does not seem to get its smtp server settings from Mail.app (maybe it used to, but not any longer). You have to set these yourself. The relevant documentation can be found here: http://www.postfix.org/SOHO_README.html#fantasy

These settings work:

# Set external SMTP relay host here, IP or hostname accepted along with a port
relayhost = smtp.myispserver.com:465
# Set username and password
smtp_sasl_password_maps = static:myusername:mypassword
# Enable auth
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
# Turn on tls encryption
smtp_tls_security_level = encrypt
header_size_limit = 4096000

# For port 465
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt

# For rewriting [email protected] to [email protected]
smtp_generic_maps = hash:/etc/postfix/generic

If outgoing port 25 is blocked, use 465 or 587. My suspicion that the smtp server refused to accept connections with incorrect sender "From: " headers was right. The response was: Sender address rejected: Domain not found (in reply to RCPT TO command). Use smtp_generic_maps to rewrite.

Check smtp and smtpd logs with:

log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info
1
  • 3
    Superb detail, thanks for the answer and more importantly the log search syntax to help debug this.
    – bmike
    Commented Jul 30, 2022 at 12:37
1

Found this reddit thread that discusses the issue and offers this solution:

"If you have Mail.app configured with an account, you can do the following: echo "Hello World" | mail -s "BCC Testing email" [email protected] -c [email protected] -b [email protected]

The -c is for carbon copy and -b for blind copy."

Also, people in this thread say that you need a utility to do this and recommend Sendemail. If Mail.app still isn't working for you after trying the solutions, try using Sendemail and see if it makes a difference.

2
  • 2
    You may be thinking of sendemail vs sendmail, sendmail is a compatibility shim for the Apple-shpped full SMTPd Postfix. Commented Jul 27, 2022 at 21:46
  • I had already checked out sendemail and it works perfectly. I was just wondering why mail no longer works. Is it because it does not allow me to set the "From: " header causing the relayhost (in /etc/postfix/main.cf) to not accept that email?
    – rgos
    Commented Jul 28, 2022 at 18:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .