Configure Office 365 Email on Linux using Postfix

Hey Folks✌️ welcome to another post, Here we discuss using Postfix to send email by command line.
Wondering to send alerts from the command line to Email, here we go …
Introduction:
Postfix is a popular and widely used open-source mail transfer agent (MTA) software. An MTA is responsible for routing and delivering email messages within a computer network, whether that network is the Internet or an internal network within an organization
👇Requirements:
- Email credentials (Microsoft 365/Gmail)
- Linux Machine
Step 1 — Install required packages:
# For Redhat based
sudo yum install postfix cyrus-sasl-plain mailx -y
# For Debian Based
sudo apt install postfix cyrus-sasl-plain mailx -yStep 2 — Change the main.cf:
Open the /etc/postfix/main.cf file using your favorite editor(Vim/nano/) and add the below lines:
vim /etc/postfix/main.cf
relayhost = [smtp.office365.com]:587
mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only
smtp_use_tls = yes
smtp_always_send_ehlo = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_generic_maps = hash:/etc/postfix/generic
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relayStep 2 — Set Password for Email:
Edit the password file /etc/postfix/sasl_passwd
For Office 365 Email-ID use like below
vim /etc/postfix/sasl_passwd
[smtp.office365.com]:587 [email protected]:PASSWORD
Step 3 — Configure Multiple Emails [Optional]
if you need multiple emails add them like below
vim /etc/postfix/sasl_passwd
[smtp.office365.com]:587 [email protected]:PASSWORD [smtp.office365.com]:587 [email protected]:PASSWORD [smtp.office365.com]:587 [email protected]:PASSWORD
Step 4— Configure the Sender Relay [ Required only for multiple Emails]
Edit /etc/postfix/sender_relay add like below
vim /etc/postfix/sender_relay
[email protected] [smtp.office365.com]:587 [email protected] [email protected] [email protected] [email protected]
Step 5— map the files to postfix
Once all is done, map the files into Postfix. Run the below commands
$ postmap main.cf $ postmap sasl_passwd $ postmap sender_relay
Step 6— Send Email
To send Email, run like below command.
$ echo "This is the Test Email" | mailx -r "[email protected]" -s "This test mail 98" [email protected]Step 6 — Check Logs [Optional]
For debugging log file is located on /var/log/maillog.
Open log file: tail -f /var/log/maillog
✅Mailx Usage:
- -a → add attachment
- -b → add Bcc Email
- -c → add CC Email
- -r → From Email Address
- -s → Subject of the Email
Taking the message from a file
$ mailx -s "Test Mail by mailx" [email protected] < file.txtSending the same Mail to Multiple Recipients:
We can send the same email to multiple receivers (not by cc or bcc) as follows:
$ mailx - s "Test Mail by mailx" [email protected], [email protected] < file.txtAdding Attachments
$ mailx - s "Test Mail by mailx" [email protected] -a File.txtAdding CC & BCC
$ mail - s "A mail sent using mailx" [email protected] -c [email protected] -b [email protected]Thanks for reading & Follow for More ✌️
Medium — https://medium.com/@karthidkk123 LinkedIn — https://www.linkedin.com/in/karthick-dkk/ Instagram — https://www.instagram.com/karthick_dkk_dk/ GitHub — https://github.com/karthick-dkk







