Search code examples
emailsmtpubuntu-18.04google-workspaceubuntu-server

How to send email of a GSuite/Google Workspace alias domain externally (e.g. via Ubuntu)


Scenario

I have domain.com on GSuite. Then I have aliasdomain.com (which is set as an alias domain for that in GSuite.) And I have a GSuite User admin (which is both admin@domain.com and admin@aliasdomain.com just like I want them to be. Emails sent to either are received via gmail's admin@domain.com account, all good.

Now I have an Ubuntu 18.04 Server from which I would like to send mail as admin@aliasdomain.com. I have learned that I need to use Google SMTP to accomplish that.

What I've tried

I've also learned that nullmailer is light and since I only wish to send a mail very occasionally, it is perfect, so I'd like to use that preferably, but Postfix or sendmail can also be an option, if that doesn't work.

Unfortunately I have very limited understanding of anything related to mail transfer agents, etc. But I've tried my best to search for a resource or a guide for this particular scenario, but most of them are quite advanced to me, and they assume the reader to be very well-versed.

What I need

How do I set up sending mail using the above scenario? I am hoping someone kind enough to guide me in this regard.

Thank you very much.


Solution

  • OK, after some trial and error, I finally got it all working. Here's a step-by-step tutorial (sort of) if anyone is interested:

    1. G SUITE/WORKSPACE:
      1. Log in to admin@domain.com's gmail and enable Send mail as (refer to its section of Step 3 here, under Send messages from your alias address
      2. Log in to domain.com's Google Admin Console > Apps > GSuite Google Workspace > GMail > End user access > Enable Allow per-user outbound gateways
    2. UBUNTU 18.04 SERVER:
      1. Install nullmailer sudo apt-get install nullmailer (then accept all default values for now, we're changing them below)
      2. cd /etc/nullmailer
      3. sudo nano defaultdomain and replace all text with aliasdomain.com
      4. sudo nano adminaddr and replace all text with admin@aliasdomain.com
      5. sudo nano remotes and replace all text with smtp.gmail.com smtp --port=465 --auth-login --user=admin@domain.com --pass=YOURPASSWORDFORadmin@domain.comGMAILACCOUNT --ssl (Read important security note below before doing this step!)
      6. sudo systemctl enable nullmailer
      7. sudo systemctl start nullmailer
    3. NULLMAILER:
      1. Nullmailer should now be ready
      2. Test it: printf "Subject: Nullmailer test\nFrom:admin@aliasdomain.com\nTo:example@hotmail.com\n\nTest from Nullmailer" | sendmail -v example@hotmail.com
      3. Check example@hotmail.com Inbox for email from admin@aliasdomain.com (you might have to look in Spam folder and mark it as Safe for future OR see new SPF record addition below.)

    IMPORTANT SECURITY NOTE

    Before attempting Step 2.5, You must either enable two-factor authorization for admin@domain.com and use App Passwords OR enable Less secure apps. It's your decision ultimately.

    SPF DNS RECORD

    You may have to include domain.com and aliasdomain.com in the an SPF TXT RECORD to.