Specify the from User When Sending Email Using the Mail Command

Specify the from user when sending email using the mail command

http://www.mindspill.org/962 seems to have a solution.

Essentially:

echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com

How to set the From email address for mailx command?

You can use the "-r" option to set the sender address:

mailx -r me@example.com -s ...

Change the From: address in Unix mail

In my version of mail ( Debian linux 4.0 ) the following options work for controlling the source / reply addresses

  • the -a switch, for additional headers to apply, supplying a From: header on the command line that will be appended to the outgoing mail header
  • the $REPLYTO environment variable specifies a Reply-To: header

so the following sequence

export REPLYTO=cms-replies@example.com
mail -aFrom:cms-sends@example.com -s 'Testing'

The result, in my mail clients, is a mail from cms-sends@example.com, which any replies to will default to cms-replies@example.com

NB: Mac OS users: you don't have -a , but you do have $REPLYTO

NB(2): CentOS users, many commenters have added that you need to use -r not -a

NB(3): This answer is at least ten years old(1), please bear that in mind when you're coming in from Google.

Send email via mail command in python script

The immediate problem is that strings in the shell need to be quoted if they contain newlines or etc. See When to wrap quotes around a shell variable.

But more fundamentally, you really don't want to use os.system here like this. Like its documentation already tells you, you generally want to prefer subprocess.

import subprocess

subprocess.run(
["mail", "-s", "Test email", "name@server.com"],
input=text, text=True, check=True)

or use smtplib to send email natively (or, conversely, don't use Python at all if all you need is a simple shell script, though you'd still need to fix the quoting then).

mail is poorly portable so if you haven't tested this on your system already, it might have additional problems.
Perhaps see also How do I send a file as an email attachment using Linux command line?

How to set sender name in mailutils while preserving the sender address

The -r option sets the envelope sender. Probably try

mailx -s 'Test Email' -a 'From: SenderName <noreply@domain.net>' myaddress@example.com <<<"this is a body"

You may wish to also separately set the envelope sender, but this adds a proper From: header which controls what gets displayed more directly.

Some MUAs might still display something different if there is a separate Sender: header, which some systems automatically add when you override the default. If you need detailed control over these things, you will probably also need to separately configure your MTA (Postfix, Sendmail, what have you).



Related Topics



Leave a reply



Submit