Send email correctly from Ubuntu VPS

Today getting a VPS becomes more and more cheap. I believe every programmer has his/her own VPS. Sometime you want to run a blog on the VPS, thus you may need to send email such as reports from this VPS too.

To send email from the VPS correctly, you firstly need a clean IP. It requries:

(1) The IP has its own PTR record, for instance:

  # dig -x 64.233.162.27 +short
 li-in-f27.1e100.net. 

Here shows the IP 64.233.162.27 has a valid PTR.

Without PTR you even can’t send email to most big players such as google.

In most cases you can setup IP’s PTR via the provider’s management panel.

(2) The IP has not been listed into any RBL blacklist.

RBL means realtime black lists, they are used for antispam purpose worldwide.

You must make sure your IP isn’t get blacklisted by any RBL.

Please validate it from the site:

http://www.anti-abuse.org/multi-rbl-check/

(3) Your sending domain has the IP added into SPF record.

For example, the sending domain is: myhostnames.com, which wants to send email from the IP 185.213.172.15, thus it should have this SPF record setup in DNS:

myhostnames.com. 300 IN TXT "v=spf1 include:_spf.google.com  ip4:185.213.172.15 ~all" 

Without the SPF setting most recipients may reject your messages, because they don’t know if the domain has the permission to use this IP for sending.

After you have finished setup the IP, you just enable MTA relay on localhost, and send email from your scripts to this relay. MTA relay will route your messages out from localhost to other recipients.

The important step is to make sure you have correct hostname to the VPS, and the hostname should be resolvable, either via DNS or hosts file.

Then, install postfix as relay on localhost:

# apt install postfix
# vi /etc/postfix/main.cf

The last two lines should be changed to:

 inet_interfaces = 127.0.0.1
 inet_protocols = ipv4 

The first line make sure postfix run on localhost only for security reasons. The second line let postfix send mail only via ipv4 protocol, using ipv6 protocol google’s mail servers may reject to talk to us.

Finally restart postfix:

# /etc/init.d/postfix restart

The MTA relay should be working now.

How to send email from within a script? It has many methods. I can show you the perl way below.

We want to install cpanm and the needed modules.

# apt install cpanminus
# cpanm Email::Send::YYClouds
# cpanm LWP::Simple
# cpanm JSON

Then write a script to send statistics report for blog visit.

#!/usr/bin/perl
 use strict;
 use POSIX;
 use Email::Send::YYClouds;
 use LWP::Simple;
 use JSON;
 use utf8;
 
 my $date = strftime "%F",localtime;
 my $subject = "Daily visit statistics - $date";
 
 my $log = "/var/log/apache2/access.log";
 my %hash;
 
 open HD,$log or die $!;
 while (<HD>) {
     # a regex for matching the correct visit IP
     next unless m{^(\d+\.\d+\.\d+\.\d+).*\"GET /.*?/ HTTP/};
     my $ip=$1; 
     $hash{$ip} ++;
 }
 close HD;
 
 my $body = '<table style="width:100%">' . "\n";
 
 for my $ip (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
     my $num = $hash{$ip};
     my $loc ="";
 
     if ($num>=2) {
         # get IP location via public api
         $loc = ip_loc($ip);
     }
 
     $body .= <<DATA;
 <tr>
   <td>$ip</td>
   <td>$num</td>
   <td>$loc</td>
 </tr>
 DATA
 }
 
 $body .= '</table>';
  
 #
 # send email
 #
 my $msg = Email::Send::YYClouds->new();
 $msg->send(recepient => ['[email protected]'],
            sender => '[email protected]',
            smtprelay => 'localhost',
            subject => $subject,
            body => $body,
       );

 #
 # ip query
 #
 sub ip_loc {
     my $ip = shift;
     my $content = get("https://ipapi.co/$ip/json/");
     if (defined $content) {
         my $json = decode_json($content);
         return $json->{city}. ", " . $json->{region}. ", ". $json->{country};
     } else {
         return "";
     }
 }

After you get message, the content looks similar to this table:

66.249.69.4213Ashburn, Virginia, US
66.249.69.4411Ashburn, Virginia, US
66.249.69.469Ashburn, Virginia, US
13.66.139.1304Redmond, Washington, US
113.88.13.164Shenzhen, Guangdong, CN
66.249.73.1003Ashburn, Virginia, US
40.77.167.102Boydton, Virginia, US
217.160.42.1492Frankfurt am Main, Hesse, DE
207.46.13.2312Redmond, Washington, US
185.85.189.2412Bursa, Bursa, TR
66.249.73.1032Ashburn, Virginia, US
121.46.142.2442Guangzhou, Guangdong, CN
62.210.83.42Paris, Île-de-France, FR
13.66.139.1282Redmond, Washington, US
134.159.238.122Kwai Chung, Tsuen Wan District, HK
176.124.231.762Rivne, Rivne, UA
167.114.100.1602Montreal, Quebec, CA
66.249.73.972Ashburn, Virginia, US

Until now, all is done well.

Print Friendly, PDF & Email