Linux

[MAILX ERROR: STATUS=BOUNCED] Fixing Mailx error when sending emails from Command line

While trying to send an email from CLI on a local server to an external email address, you can come across the error log below.

echo -e "testing email via yourisp.com from command line\n\nsent on: $(date)" | mailx -r "Test-Email <ola@testlab.com>" -s "test cli email $(date)" -- meettoy2004@gmail.com

To fix this, make sure you have the SMTP server address (either Gmail or your ISP) and also authentication to log in (Some ISP’s allow anonymous) then use the command below

echo "This is a test email on CLI initiated using mailx" | mailx -vvv -s "Test mail" -r "ola@testlab.com" -S smtp="your smtp server address" meettoy2004@gmail.com

USING GMAIL AS THE EMAIL SERVER
– Create a certificate directory then create new certificate and key databases

mkdir ~/.certs
certutil -N -d ~/.certs

Download certificate from Gmail and import the cert file into the new database

echo -n | openssl s_client -connect smtp.gmail.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/gmail.crt
certutil -A -n "Google Internet Authority" -t "C,," -d ~/.certs -i ~/.certs/gmail.crt

Add configurations into the mailx config file (/etc/mail.rc)

 
account gmail {
set smtp-use-starttls
set ssl-verify=ignore
set smtp-auth=login
set smtp=smtp://smtp.gmail.com:587
set from="username@gmail.com"
set smtp-auth-user=username@gmail.com
set smtp-auth-password=jjdeuen@SGY^
set ssl-verify=ignore
set nss-config-dir=/home/path/to/.certs
}

Test Email.

echo -e " body " | mailx -A gmail -s "Subject" recipient@anywhere.com

I hope you found this blog post helpful. If you have any questions, please let me know in the comment session.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x