Mass Emails Sending From Bash Script in Linux

To prevent abuse and don’t allow to send bulk emails through SMTP server, it is recommended to restrict the number of messages that each user can send.

Let’s say we have limited the number of messages to send, but how can we check that the limitation works?

The best way to make such check is to try to send mass emails, as spammers usually do, using our SMTP server as relay.

From the Linux Command Line, we can create a simple Bash Script that can send multiple emails for testing purposes.

Sending Bulk Emails for Testing Purposes

Use the following Bash Script from your Linux machine to send 1000 emails to example@mail.tld:

#!/bin/bash
for i in {1..1000}; do
	echo "Test" | $(which mail) -s "Test Message №$i" "example@mail.tld"
done

Postfix: Gmail as Relay – Linux Mint/Ubuntu/Debian

Many ISPs block sending email over port 25. This means that you won’t be able to send mail directly from your Linux server.

A good way to get around this limitation is to set up a relay through a Gmail account.

1. Install the Required Packages

Execute the following command to install required packages:

$ sudo apt-get install mailutils

During the installation, a popup box may display. If it does, select your server as Internet Site and for FQDN use something like domain.tld

2. Configure Postfix to use Gmail as Relay

Edit the Postfix configuration file:

$ sudo vi /etc/postfix/main.cf

Add the following lines:

# Relaying Postfix SMTP via GMAIL
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

3. Setup Gmail Credentials

Open (create) the file sasl_passwd:

$ sudo vi /etc/postfix/sasl_passwd

Add the following line (set your Gmail username and password):

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

Set the permissions and tell Postfix to use the sasl_passwd file:

$ sudo chmod 400 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd

4. Validate the Certificate

Add the certificate to Postfix, as follows:

$ cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

5. Test Postfix with Gmail SMTP Realy

Restart Postfix to apply all changes.

$ sudo service postfix restart

Try to send an email.

$ echo "Hello World" | mail -s "Test Message" you@example.com

Check the mail log:

$ tail /var/log/mail.log

If all goes well, you should not see any errors.

Once configured, all emails from your server will be sent via Gmail.

The Gmail’s SMTP Server has a limit of 500 emails per day. So use it wisely!

HowTo: Retrieve Email from a POP3 Server using the Command Line

The POP3 (Post Office Protocol Version 3) is an application-layer Internet standard protocol used by local Email clients to retrieve Email from a remote server over a TCP/IP connection.

This tutorial shows how to connect to POP3 mail server and manage incoming Email using the ‘telnet’ command.

Step 1: Open a connection from your computer to a POP3 mail server

$ telnet pop.domain.ext 110
Trying ???.???.???.???...
Connected to pop.domain.ext.
Escape character is '^]'.
+OK ready
  • pop.domain.ext – Your mail server;
  • 110 – Default port for POP3.

Step 2: Type your Login

> USER username
+OK Password required for UserName.

Step 3: Type your Password

> PASS password
+OK username has ? visible messages (? hidden) in ????? octets.

POP3 Commands with Description

Here are the basic POP3 commands, that you can use to manage your incoming Email.

Command Description Example
USER [username] 1st login command USER Stan
+OK Please enter a password
PASS [password] 2nd login command PASS SeCrEt
+OK valid logon
QUIT Logs out and saves any changes QUIT
+OK Bye-bye.
STAT Returns total number of messages and total size STAT
+OK 2 320
LIST Lists all messages LIST
+OK 2 messages (320 octets)
1 120
2 200

LIST 2
+OK 2 200
RETR [message] Retrieves the whole message RETR 1
+OK 120 octets follow.
***
DELE [message] Deletes the specified message DELE 2
+OK message deleted
NOOP The POP3 server does nothing, it merely replies with a positive response. NOOP
+OK
RSET Undelete the message if any marked for deletion RSET
+OK maildrop has 2 messages (320 octets)
TOP [message] [number] Returns the headers and number of lines from the message TOP 1 10
+OK
***

HowTo: Send Email from an SMTP Server using the Command Line

The SMTP (Simple Mail Transfer Protocol) is an Internet standard for electronic mail (Email) transmission across Internet Protocol (IP) networks.

This tutorial shows how to connect to SMTP mail server and send an Email using the ‘telnet’ command.

Step 1: Open a connection from your computer to an SMTP mail server

$ telnet smtp.domain.ext 25
220 smtp.domain.ext ESMTP Sendmail ?version-number?; ?date+time+gmtoffset?
  • smtp.domain.ext – Your mail server;
  • 25 – Default port for SMTP.

Step 2: Declare your domain name or IP-address

You can set local.domain.name or anything you want in ‘HELO’, because the server doesn’t check its authenticity, that is one of the drawbacks of this protocol.

> HELO local.domain.name
250 smtp.domain.ext Hello local.domain.name [xxx.xxx.xxx.xxx], pleased to meet you

Step 3: Set sender’s email

> MAIL FROM: sender@adress.ext
250 2.1.0 sender@adress.ext... Sender ok

Step 4: Set recipient’s email

> RCPT TO: recipient@adress.ext
250 2.1.5 recipient@adress.ext... Recipient ok

Step 5: To write the message, type DATA and press ‘Enter’

> DATA
354 Enter mail, end with "." on a line by itself

Step 6: On the first line type ‘SUBJECT: Your Subject’ and press ‘Enter’ twice

> SUBJECT: Test message

Step 7: Continue typing your message

Hello,
this is a TEST message, 
please don't reply.
Thank you.

Step 8: Put a single period (.) on a line by itself and press ‘Enter’ to send your message.

> .
250 2.0.0 ???????? Message accepted for delivery

Step 9: Close the connection

> QUIT
221 2.0.0 server.com closing connection