Raspberry PI - Send mail from command line using GMAIL smtp server - command-line

How can I send email from Raspberry Pi using my gmail account?
I would like to send mail from command line and use this method in my scripts.
Envirenment:
Hardware: Raspberry PI 3
OS: Jessie
SMTP: smtp.gmail.com

I use this method on my Raspberry Pi 3 devices:
Google account setting
Login to your gmail account
Go to: Settings -> Accounts and Import -> Other Google Account settings
Go to: Personal info & privacy -> Account overview
Go to: Sign-in & security -> Connect apps & sites
Set option Allow less secure apps to ON
Install SSMTP
sudo apt-get install ssmtp
Save original conf file
sudo mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.bak
Create new conf file (with vi, or some other text editor)
sudo vi /etc/ssmtp/ssmtp.conf
file content
root=your_account#gmail.com
mailhub=smtp.gmail.com:587
FromLineOverride=YES
AuthUser=your_account#gmail.com
AuthPass=your_password
UseSTARTTLS=YES
UseTLS=YES
# Debug=Yes
Secure conf file
sudo groupadd ssmtp
sudo chown :ssmtp /etc/ssmtp/ssmtp.conf
If you have error on this step like ''cannot access'' ... you must find ssmtp file and use that path: sudo find / -name "ssmtp"
sudo chown :ssmtp /usr/sbin/ssmtp
sudo chmod 640 /etc/ssmtp/ssmtp.conf
sudo chmod g+s /usr/sbin/ssmtp
Sending mail from (only one) command line
echo "This is a test" | ssmtp recipient.address#some_domain.com
or
printf "To: recipient.address#some_domain.com\nFrom: RaspberryPi3\nSubject: Testing send mail from Raspberry\n\nThis is test. Best Regards!\n" | ssmtp -t
Sending mail from file test.txt
Make file with similar content:
To: recipient.address#some_domain.com
From: your_account#gmail.com
Subject: Testing send mail from Raspberry
This is test mail (body)
Best Regards!
Now you can send mail from file
ssmtp recipient.address#some_domain.com < test.txt
That's all :)

Related

uploading files in email without restriction

How can I upload a file blocked by email providers?
I want to upload certain programs in my email. but, email provider is blocking it. Any way to workaround this problem?
In linux:
apt install sharutils
uuencode input_filename /dev/stdout > output_filename
use pico to remove following first line from <output_filename>
begin 644 /dev/stdout
upload in email
when downloaded
use pico to add following line to top of <encoded_filename>
begin 644 /dev/stdout
uudecode encoded_filename > decoded_filename

SSMTP fails after migrating to Buster

SSMTP was working for years now on my RPi - latest on stretch.
Now I migrated to buster - and after this step it now fails.
$ cat /etc/ssmtp/ssmtp.conf
root=user#mydomain.com
mailhub=smtp.gmail.com:587
hostname=localhost
UseSTARTTLS=YES
AuthUser=user#gmail.com
AuthPass=very_secret
FromLineOverride=YES
$ echo "MailBody"|mail --debug-level=3 -s "My Subject" mailaddress#domain.com
mail: sendmail binary: /usr/sbin/sendmail
mail: source=system, name=user, passwd=x, uid=1001, gid=1000, gecos=user,,,, dir=/home/user, shell=/bin/bash, mailbox=/var/mail/user, quota=0, change_uid=1
mail: source=system, name=user, passwd=x, uid=1001, gid=1000, gecos=user,,,, dir=/home/user, shell=/bin/bash, mailbox=/var/mail/user, quota=0, change_uid=1
mail: mu_mailer_send_message(): using From: user#localhost
mail: Sending headers...
mail: Sending body...
mail: /usr/sbin/sendmail exited with: 1
mail: progmailer error: Process exited with a non-zero status
mail: cannot send message: Process exited with a non-zero status
mail: source=system, name=user, passwd=x, uid=1001, gid=1000, gecos=user,,,, dir=/home/user, shell=/bin/bash, mailbox=/var/mail/user, quota=0, change_uid=1
$ echo $?
36
I have a lot of shell-scripts sending mails with commands like:
echo "MailBody"|mail -s "My Subject" emailaddress#domain.com
=> so if SSMTP is no longer supported, I would neeed a solution following that syntax ...
thanks for any help!
I fixed this by switching away from ssmtp (which some sources say being unmaintained) to msmtp (https://wiki.debian.org/msmtp).
This could or could not apply to you, try to check the error code by sending an email directly via /usr/sbin/ssmtp -v ...., I had exit 36 (echo $? to get the code).
If you are in a similar situation and want to give msmtp a try, these the steps I followed:
Install:
$ sudo apt remove ssmtp
$ sudo apt install msmtp msmtp-mta
Configure (I'm not configuring GMail, I use Dreamhost email service, so your details might be different):
$ cat /etc/msmtprc
# Set default values for all following accounts.
defaults
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /tmp/msmtp.log
# Raspberry Pi
account rasp
host smtp.dreamhost.com
port 465
from raspberry#<REDACTED>
user raspberry#<REDACTED>
password <PLAINTEXTPASSWORD>
# default account
account default : rasp
For comparison, this was my /etc/ssmtp.conf file:
root=postmaster
mailhub=smtp.dreamhost.com:465
AuthUser=raspberry#<REDACTED>
AuthPass=<REDACTED>
UseTLS=YES
UseSTARTTLS=YES
TLS_CA-File=/etc/ssl/certs/ca-certificates.crt
hostname=<REDACTED>.org
FromLineOverride=NO
Set msmtp as default MTA. I've added the last line to my mail.rc file:
$ cat /etc/mail.rc
set ask askcc append dot save crt
ignore Received Message-Id Resent-Message-Id Status Mail-From Return-Path Via Delivered-To
set mta=/usr/bin/msmtp
[NOTE]: There is also an issue with msmtp in that it's not populating automatically the "From:" header in the message so emails get rejected, to fix it, when calling mail make sure to do it manually:
echo TEST | mail -a "From: raspberry#<REDACTED>" --debug-level 10 -s '[rasp] server' <REDACTED>#gmail.com
This makes email sending work again from my Raspberry Pi on Debian buster/sid.

Upgrade to php7 now mail() isn't working

from phpinfo file
sendmail_from no value no value
sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
Also installed a new Debian Stretch server.
SMTP port is open in the iptables.
I realised that I needed to install and configure exim.

CFG Error email receiving every 5 minutes from Zabbix on Unix server

I have configured sSMTP on myzabbixserver Unix system which successfully delivers Zabbix alerts via Gmail. Strangely I am receiving an email in my Gmail inbox (which I am using as From address for sSMTP config) EVERY 5 minutes with following details:
from: root <my.zabbix#gmail.com>
to: root
bcc: my.zabbix#gmail.com
subject: Cron <root#myzabbixserver> if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then mkdir -p /var/log/mrtg ; env LANG=C /usr/bin/mrtg /etc/mrtg.cfg 2>&1 | tee -a /var/log/mrtg/mrtg.log ; fi
body: ERROR: CFG Error in "workdir", line 8: Working directory /var/www/mrtg does not exist
sSTMP.conf
root=my.zabbix#gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
AuthUser=my.zabbix#gmail.com
AuthPass=my.password
hostname=myzabbixserver
FromLineOverride=YES
UseTLS=YES
UseSTARTTLS=YES
AuthMethod=LOGIN
gmail.sh script which Zabbix uses to send email alerts:
#!/bin/bash
to=$1
subject=$2
body=$3
echo $body | /usr/bin/mailx $to -s "$subject"
What does that email mean and how can I stop it ?
This is not a Zabbix problem, but mrtg problem. Your mrtg cron job /etc/cron.d/mrtg is failing and you are receiving this cron error output.
Solutions:
configure mrtg correctly (http://www.debianhelp.co.uk/mrtg.htm)
uninstall mrtg/disable mrtg cron job
...

Sending mail via shell script

I want to send a zip file via email on shell script. I know I need to use the following command:
cat file.txt | mail -s "This is subject" [email]name#address.com[/email]
I always get this error:
line 13: mail: command not found
Does anyone know where and how I can install the mail command?
I am using Cygwin to test my shell script.
If you are in ubuntu, you can use apt
sudo apt-get install mailutils postfix
Install the "nail" or "mailx" package.