28

How do I make postfix send emails from user@mydomain instead of root@hostname? Even after installing and entering my domain when it asked, it's still being sent with the hostname and not the domain I provided. In my main.cf file

myorigin = /etc/mailname

and /etc/mailname contains:

gateblogs.com

which is my domain.

I have managed to fix my problem temporarily by changing my hostname to my domain name. However, how can I change who the email is from; currently mail is shown from root I want it to be something else.

8
  • How are you sending the email? Using the mail command or what?
    – Wildcard
    Commented Jul 18, 2017 at 4:56
  • Yes. I'm using echo "body" | mail -s blah email Commented Jul 18, 2017 at 4:59
  • 6
    mail -r fromaddress. See man mail.
    – Wildcard
    Commented Jul 18, 2017 at 5:02
  • How do I fix mail coming from myhostname.mac.com? Commented Jul 18, 2017 at 5:07
  • Did you read my previous comment? Did you check the man page and read about the -r option? Is that what you were looking for?
    – Wildcard
    Commented Jul 18, 2017 at 5:28

8 Answers 8

23

Was having the same request and finally found and answer here (Would have flag this one as a duplicate)

User Rustyx at askubuntu answered this :

To use a local domain other than the hostname in Mailutils:

Create a file /etc/mailutils.conf with the following contents:

address {
  email-domain somedomain.com;
};

You can see what else can be configured in mailutils.conf with:

$ mail --config-help

Please, take time to upvote his answer if you feel it helps you.

1
  • Make sure you remember to change the domain, for the love of facepalm.
    – Nik LePage
    Commented Aug 29 at 13:30
14

The question is five years old, but I just had to set this up.

Issue:

I have a root@myserver that needs to be forwarded to my Office365. This requires my outgoing email on the server to always have FROM set to my 356 account.

In other words, when root@myserver sends an email, the FROM has to be re-written as [email protected].

Answer:

This re-write can be handled via a /etc/postfix/generic file.

In the postfix/main.cf add the following

smtp_generic_maps = hash:/etc/postfix/generic

Then create a /etc/postfix/generic file as below and add additional users if needed.

root@localdomain [email protected]
@localdomain [email protected]
root@myserver  [email protected]
userA@myserver [email protected]

Run Postmap and restart

sudo postmap /etc/postfix/generic
sudo systemctl restart postfix

All outgoing emails from both root@myserver and userA@myserver will automatically have the FROM SMTP field sent as [email protected].

2
  • Thanks. Wish I knew why one machine needed this and the other didn't... weird stuff. It works though!
    – kevins
    Commented Apr 17, 2023 at 21:16
  • If that doesn't work yet, and you get a warning like this in /var/log/mail.log: SendAsDenied; [email protected] not allowed to send as [email protected]; create a /etc/postfix/sender_canonical file with content like this [email protected] [email protected]. Then run postmap /etc/postfix/sender_canonical and restart the service again. Commented Jan 26 at 15:50
13

Postfix itself does not "set" the from address for an mail (as long has you haven't really tweaked the postfix configuration).
The from address header of an e-mail is set by the mail client which is asking postfix to deliver the mail (Postfix is a MTA).

Hence you are looking at the wrong place - as far as I understand your question.

You mentioned that you are using the mail command to test your configuration. This neat little command is - by default - using the systems user name under which the command gets executed. In your case this seems to be user root. Try executing mail as a different user and you'll see the from part changed.
And since the command mail does - by default - not append the domain part to the "from" header of the mail it hands over to postfix, postfix is appending the myorigin part automatically to root.

Though mail is not limiting you to not use some other "from" e-mail header. You can read about on the www or the manual page of mail. Also consider using sendmail.

Please note that postfix is a beast when it comes to configurability. You may accomplish almost everything you want postfix to do if you really understand its architecture and configuration files.
But since you are asking a quite 'newbee-ish' question you may not yet want to go down that road...

1
  • 1
    And for the record, mail -r [email protected] overrides the default behavior. Why do you suggest using sendmail? The relationship between mail, sendmail and postfix are pretty fuzzy. There is this question, but after reading all answers I feel almost more confused.
    – bluenote10
    Commented May 12, 2021 at 13:52
7

You asked this twice, but I'll repeat my answer. In main.cf:

myorigin = $mydomain
4

Using gue's and Bob Eager's answer, I figured out that the following setting in /etc/postfix/main.cf also does the job (on an Ubuntu 18.04 machine):

# appending .domain is the MUA's job.
# append_dot_mydomain = no  # --> this is the default setting
append_dot_mydomain = yes  # --> Setting it to yes appends the domain to the hostname

It also explains what gue said: appending the .domain is the Mail User Agent's job. By setting it to yes we are overriding this.

So, my setup is :

[/etc/postfix/main.cf]
...
append_dot_mydomain = yes
...
myorigin = /etc/mailname
...

[/etc/mailname]
mydomain.example.com

Still, it doesn't feel like the right way; after all by setting it to yes we are violating the principle that "appending the .domain is the Mail User Agent's job"

Furthermore, I am not sure why I need to configure it this way to make it work. I have another server (configured by someone else; a Debian 8.11 machine), which does append the domain after the hostname correctly, even with /etc/mailname being set to the domain, and with append_dot_mydomain=no. And on that server it works both for mailx and for mails sent by e.g. logcheck

2

If you are sending mail with a PHP script: You can set the "from" portion of the header before you execute the mail() command.

e.g.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .   // this will show up as the from address
    'Reply-To: [email protected]' . "\r\n" .        // set this as the senders email so replys will go to them
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

This is how I have stepped around this problem in my particular situation.

Depending on your situation just setting these headers should be the solution to your issue.

0

You should consider reading https://www.unix-experience.fr/postfix/rewrite_sender_dest/

sender_canonical_maps = hash:/etc/postfix/sender_canonical
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
0

Issue:

I have a root@myserver that needs to be forwarded to different user. This requires my outgoing email on the server to always have FROM set to Account1 instead of ROOT

In other words, when root@myserver sends an email, the FROM has to be re-written as [email protected].

Answer:

This re-write can be handled via a /etc/postfix/generic file.

In the postfix/main.cf add the following

smtp_generic_maps = hash:/etc/postfix/generic Then create a /etc/postfix/generic file as below and add additional users if needed.

root@localdomain [email protected] @localdomain [email protected] root@myserver [email protected] user1@myserver [email protected]

Run Postmap and restart

sudo postmap /etc/postfix/generic sudo systemctl restart postfix

All outgoing emails from both root@myserver and user1@myserver will automatically have the FROM SMTP field sent as [email protected].

*****Be sure to substitute myserver with example.com (your server). I made that mistake at first ********

New contributor
Photo Larry is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

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