2

I need to send server reports to myself from my remote servers, and I don't mind them going to spam, so I don't need SPF, DKIM, etc.

I tried using mailutils to send something like this: uptime | mail -s "uptime" foo@bar.com, but the emails don't go through. In /var/mail/root I received a message saying the HELO was invalid. So then I tried also adding -r root@my-ip, and now I don't get any error messages but I don't receive the email either.

How can I do this?

  • Where did you get this message, on the sending or on the receiving machine? – Olaf Dietsche May 31 '14 at 10:46
  • @OlafDietsche Sending machine. – ChocoDeveloper May 31 '14 at 13:15
2

I use the exim4 email package on Ubuntu configured as a "smart host". This means you give exim4 the user name and password for your normal SMTP server and it will send the email (from commands like mail and sendmail) through that.

Here is the question I asked about setting it up on AskUbuntu.com and the answer that I found:

I finally found a detailed set of instructions by Tony Scelfo that actually work. It appears that you have to use transport layer security (TLS) on port 587. I have not gotten SSL SMTP to work.

First run sudo dpkg-reconfigure exim4-config and use these config options:

  • General type of mail configuration: mail sent by smarthost; received via SMTP or fetchmail
  • System mail name: <your hostname>
  • IP-address to listen on for incoming SMTP connections: 127.0.0.1
  • Other destinations for which mail is accepted: <your hostname>
  • Machines to relay mail for: <leave this blank>
  • IP address or host name of the outgoing smarthost: mail.example.com::587
  • Hide local mail name in outgoing mail?
    • Yes - all outgoing mail will appear to come from your gmail account
    • No - mail sent with a valid sender name header will keep the sender’s name
  • Keep number of DNS-queries minimal (Dial-on-Demand)? No
  • Delivery method for local mail: <choose the one you prefer>
  • Split configuration file into small files? Yes (you need to edit one of the files next)

Then run sudo vi /etc/exim4/passwd.client and add the following lines for your mail host, and any aliases it has (found through nslookup). Substitute <email address> and <password> with the account you want to route mail through):

mail.example.com:<email address>:<password>
mail.yourhosting.provider:<email address>:<password>

Once you edit the passwd.client file, run sudo update-exim4.conf which will integrate your changes into your Exim4 config.

Run sudo /etc/init.d/exim4 restart and make sure that the service stops and starts properly. If the service is unable to restart, something probably went wrong when you edited the passwd.client file.

If Exim4 restarted, go ahead and run sudo tail -f /var/log/exim4/mainlog to watch the mail logs. In a different window, send an email from your system and make sure that you see a record go by withR=smarthost T=remote_smtp_smarthost H=gmail-smtp-msa.l.google.com ... X=TLS-1.0:RSA_ARCFOUR_MD5:16 in it. The X=TLS means that the mail is being sent with transport layer security which is what you want.

  • Thanks for the answer, but I'm not sure if I understood, you seem to be saying that I need to host my mail somewhere else? I just want to send the emails like I used to years ago: using localhost:25 and I would receive an email from eg. root@machine-name. No need for third party servers, I would only need that if I wanted to avoid going to the spam folder. – ChocoDeveloper May 31 '14 at 13:21
  • 1
    You can also configure exim to send email directly. These days such mail is often discarded because it doesnt match SPF or othewise looks like spam. It isnt just about keeping it out of the spam folder, but also about deliverability. – Stephen Ostermiller May 31 '14 at 14:05
0

This can also be done using exim on Fedora/CentOS/RHEL using gmail for SMTP (to build on Stephen Ostermiller's answer using a different distro). You first install exim with yum:

sudo yum install exim

On Fedora 20 this installed version 4.80.1. Once installed you modify the configuration located in /etc/exim/exim.conf and add the following sections to the file:

# in the routers configuration section:
send_via_gmail:
  driver = manualroute
  domains = ! +local_domains
  transport = gmail_smtp
  route_list = * smtp.gmail.com

# in the transports configuration section
gmail_smtp:
  driver = smtp
  port = 587
  hosts_require_auth = $host_address
  hosts_require_tls = $host_address

# in the authentication configuration section
gmail_login:
  driver = plaintext
  public_name = LOGIN
  client_send = : {your email address at gmail} : {password}

If you use two factor authentication in Google then you must use an app password. Once this configuration is in place you must restart the service for it to take effect:

sudo service exim restart

Once the service starts you can use the mail command, from the command line, to send email using your gmail account.

mail example@example.com
subject: {email subject} <enter>
{message}
.

Where example@example.com is your recipient. The . terminates the message and sends it via exim via Google SMTP using the credentials in the gmail login. If you don't have a gmail account then set one up to get access to the SMTP server. If you have any problems then the logs are saved in /var/log/exim/ in main.log and panic.log for errors.

  • But why do I need a gmail account? I remember doing this years ago (just not how), and I didn't need any accounts. I just sent the emails via localhost:25 and I received an email from root@machine-name. – ChocoDeveloper May 31 '14 at 13:18
0

Ok I solved it:

$ apt-get install mailutils
$ vim /etc/postfix/main.cf
myhostname = mydomain.com
$ service postfix restart

Then configure your domain's records to have "mail.mydomain.com" point to your machine's IP, MX record point to "mail.mydomain.com", and also set the SPF using the TXT record like this: v=spf1 include:mydomain.com ip4:1.2.3.4 -all (replace for actual IP)

Now you can send mails with the mail command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.