27

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.

7 Answers7

21

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.

nvidot
  • 311
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...

gue
  • 249
  • 1
    And for the record, mail -r foo@bar.com 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 May 12 '21 at 13:52
10

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 myaccount@myoffice356domain.com.

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 myaccount@myoffice356domain.com
@localdomain myaccount@myoffice356domain.com
root@myserver  myaccount@myoffice356domain.com
userA@myserver myaccount@myoffice356domain.com

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 myaccount@myoffice356domain.com.

kjpires
  • 103
  • Thanks. Wish I knew why one machine needed this and the other didn't... weird stuff. It works though! – kevins Apr 17 '23 at 21:16
  • If that doesn't work yet, and you get a warning like this in /var/log/mail.log: SendAsDenied; your.account@dom.ain not allowed to send as root@dom.ain; create a /etc/postfix/sender_canonical file with content like this root@dom.ain your.address@dom.ain. Then run postmap /etc/postfix/sender_canonical and restart the service again. – Tim Chaubet Jan 26 '24 at 15:50
6

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

myorigin = $mydomain
Bob Eager
  • 3,600
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      = 'target@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: noreply@yourdomain.com' . "\r\n" .   // this will show up as the from address
    'Reply-To: from.email@gmail.com' . "\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.

Goahnary
  • 123
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