lunedì 18 novembre 2013

Mandare mail da Raspberry Pi tramite Gmail


Aggiornamento 1: settaggi su gmail

By default, Gmail will not allow your apps to send emails using your account details. However, if you know want you are doing and want to permit your app to access Gmail’s SMTP server with your ID, you can configure it in Security settings

Generally, Gmail accounts are configured with two different authentication methods:
  1. Standard Authentication (traditional Username / Password based authentication)
  2. Two Step Verification (Username / Password and Auto-generated Auth/Verification Code)
Based on the authentication method used for your Gmail account, use one of the following methods to enable SMTP server access to your account:

1 Allowing Gmail SMTP Access for Accounts with Standard Authentication
  • Login to your Gmail account using your username and password
  • From the top right corner go to “My Account“
  • Under “Sign-in & security” section locate “Connected apps & sites” and click on it
  • Locate “Allow less secure apps” setting and turn it “On“
Once “Allow less secure apps” setting is turned on, you can access Gmail SMTP server with your Gmail account username and password.

2 Allowing Gmail SMTP Access for the Accounts with 2-Step Authentication

For sending mail using 2-Step Authentication, you may want to use a git repository with a Python script at https://github.com/pradeesi/email-from-raspberry-pi.
  • Login to your Gmail account using your username and password
  • From the top right corner go to “My Account“
  • Under “Sign-in & security” section locate “Signing in to Google” and click on it.
  • Locate “App Passwords” and click on it
  • Gmail will ask your account password for authentication. Enter the password and click on “Sign in“. After sign in, you will be presented with options to create App Password
  • From the “Select App” drop down select “Mail” and from “Select Device” drop down select “Other“
  • On selecting “Other” from ”Select Device” drop down, Gmail will ask you to enter Device name; type “Python_Script” in the text box. After this Click on “Generate” button to get the Password
  • Save this password for later use. (You will use this password along with your mail id (username) in Python script for account authentication. For your mail access from the computer or mobile, you will continue using the old password)


È utile avere la possibilità di mandare mail usando un account esterno. Qui si spiega come usare Gmail. Le configurazioni funzionano correttamente sul mio sistema:

uname -a
Linux raspberrypi 3.6.11+ #538 PREEMPT Fri Aug 30 20:42:08 BST 2013 armv6l GNU/Linux

Esempi di utilizzo in Linux/UNIX
  • L'output del crontab può essere mandato via mail mettendo all'inizio del file di crontab la linea: MAILTO=user@domain.com
  • Con la seguente linea nel file /etc/arpwatch.conf il demone arpwatch manda via mail i nuovi MAC addresses e i cambi di IP che scopre sull’interfaccia eth0eth0 –m user@domain.com

Configurazione in Raspberry Pi
I passi necessari sono:
  • apt-get update ci si assicura che gli apt repositories sono aggiornati.
  • apt-get install ssmtp si installa il pacchetto ssmtp.
  • Editare il file /etc/ssmtp/ssmtp.conf (ad esempio con vi - vedi tutorial). Le uniche linee non commentate devono essere:
root=postmaster
mailhub=smtp.gmail.com:587
hostname=raspberrypi
AuthUser=user@gmail.com
AuthPass=password
UseSTARTTLS=YES
FromLineOverride=NO

Il parametro hostname deve avere ciò che si vede con il comando hostname.
user e password sono i parametri con cui si accede a Gmail.
Il parametro FromLineOverride=YES secondo la fonte principale (vedi in basso) permetterebbe di modificare a piacere il sender della mail. Nella pratica non funziona, quindi si consiglia di configurarlo a NO.
Sempre secondo la fonte principale, nel file /etc/ssmtp/revaliases bisogna mettere tutti gli users del sistema da cui è possibile mandare mail. Nella pratica, ogni user della mia Raspberry Pi è in grado di mandare mail. Per cui si tralascia la configurazione di questo file.

Le mail avranno nel campo From il testo configurato nella quinta colonna del file /etc/passwd. Ad esempio:

cat /etc/passwd
root:x:0:0:Root:/root:/bin/bash

Le mail inviate dal user root si visualizzeranno con il mittente Root. Se la quinta colonna del file /etc/passwd è vuota, il mittente sarà visualizzato come user@gmail.com.

Esempi da linea di comando o script shell
  • echo "text" | sendmail user@domain.com manda una mail al destinatario user@domain.com con il testo text. L'oggetto della mail sarà vuoto.
  • echo "text" | sendmail user1@domain.com user2@domain.com manda una mail a due destinatari.
  • echo "text" | sendmail user1@domain.com user2@domain.com -c user3@domain.com user4@domain.com manda una mail a due destinatari e ne mette altri due in copia.
  • echo -e "Subject:subject\ntext" | sendmail user@domain.com manda una mail con l'oggetto subject.
  • echo -e "Subject:subject\nTo:user1@domain.com\nReply-To:user2@domain.com\ntext" | sendmail user1@domain.com manda una mail mettendo nel campo To: il destinatario e nel campo Reply-To: l'indirizzo a cui rispondere.
  • echo -e "Subject:subject\nTo:user1@domain.com, user2@domain.com\nCc:user3@domain.com, user4@domain.com\ntext" | sendmail user1@domain.com user2@domain.com -c user3@domain.com user4@domain.com manda una mail con l'intestazione che contiene l'informazione di chi è stato messo in To: e chi in Cc:.

Importante: alcuni sistemi bloccano le mail se il campo To: è vuoto. È quindi opportuno metterlo sempre.

Importante: per fare in modo che la sintassi precedente funzioni in un shell script, bisogna configurare una variabile con il valore "echo -e" e usare questa variabile al posto del comando echo -e:

#!/bin/sh
...
ECHO="echo –e"
$ECHO "Subject:subject\ntext"| sendmail user@domain.com
...

In un shell script si possono anche parametrizzare il testo e l'oggetto:

#!/bin/sh
...
ECHO="echo –e"
mail_text="text to send"
mail_subject="subject of the mail"
mail_to=user@domain.com
$ECHO "Subject:$mail_subject\nTo:$mail_to\n$mail_text"| sendmail $mail_to
...

Mail con attachments
Per mandare una mail con attachments si consiglia di usare mutt invece di sendmail.
  • apt-get update
  • apt-get install mutt
  • echo "text to send" | mutt -s "subjectuser1@domain.com -c user2@domain.com -a file-to-attach1.ext file-to-attach2.ext manda una mail a user1@domain.com, con copia a user2@domain.com, con oggetto subject e testo text to send, mettendo in allegato i file file-to-attach1.ext e file-to-attach2.ext.

Fonte principale: http://iqjar.com/jar/

Se utilizzi queste configurazioni e vedi che non funzionano, per favore lascia un post.
Verificherò sul mio sistema e se vuoi ti darò supporto per fare in modo che le configurazioni funzionino sul tuo.

1 commento:

  1. Ciao,
    premetto che ho acceso il Rasp 1 ora fa....

    Ricevo un errore sendmail: 550 5.1.0. pZ........ invalid domain

    Eppure ho controllato 100 volte che il dominio è scritto giusto, la porta pure

    Grazie

    RispondiElimina