mercredi 13 janvier 2016

Gladys envoie des mail ...

Comme Gladys va surveiller la maison en notre absence il faut quelle puisse envoyer des alerte s'il se passe un évènement.

Il faut donc que l'on puisse envoyer des mails depuis le RaspberryPi et même que l'on puisse envoyer des mails depuis la ligne de commande ce qui permet d'automatiser certaines surveillances système en crontab (on y reviendra).

Si vous souhaitez envoyer des données régulières par mail pour surveiller, la température de la maison, que le chien ne dévore pas le canapé, la pousse de votre camélia fraichement planté, où tout autre évènement dont vous voudriez des données régulièrement, il faut que votre RaspberryPi puisse envoyer des mail.


Aller c'est parti.

sudo apt-get update
 
sudo apt-get upgrade

Ca c'est un bon début avant toute modif.

Ensuite j'instale ssmtp qui me permetra d'accéder à un serveur smtp pour l'envoie des mails

pi@gladys:~ $ sudo apt-get install -y ssmtp
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  ssmtp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 54.2 kB of archives.
After this operation, 1,024 B of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main ssmtp armhf 2.64-8 [54.2 kB]
Fetched 54.2 kB in 0s (77.0 kB/s)
Preconfiguring packages ...
Selecting previously unselected package ssmtp.
(Reading database ... 33395 files and directories currently installed.)
Preparing to unpack .../ssmtp_2.64-8_armhf.deb ...
Unpacking ssmtp (2.64-8) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up ssmtp (2.64-8) ...



OK, ensuite j'installe mailutils qui me permettra d'envoyer des mail avec la commande mail

pi@gladys:~ $ sudo apt-get install -y mailutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  guile-2.0-libs libgc1c2 libgsasl7 libkyotocabinet16 liblzo2-2 libmailutils4 libntlm0 libunistring0 mailutils-common
Suggested packages:
  mailutils-mh mailutils-doc
The following NEW packages will be installed:
  guile-2.0-libs libgc1c2 libgsasl7 libkyotocabinet16 liblzo2-2 libmailutils4 libntlm0 libunistring0 mailutils mailutils-common
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,959 kB of archives.
After this operation, 19.3 MB of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libgc1c2 armhf 1:7.2d-6.4 [122 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libunistring0 armhf 0.9.3-5.2 [253 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main guile-2.0-libs armhf 2.0.11+1-9 [2,162 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main liblzo2-2 armhf 2.08-1.2 [47.6 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main libkyotocabinet16 armhf 1.2.76-4 [289 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main mailutils-common all 1:2.99.98-2 [599 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main libntlm0 armhf 1.4-3 [19.7 kB]
Get:8 http://mirrordirector.raspbian.org/raspbian/ jessie/main libgsasl7 armhf 1.8.0-6 [193 kB]
Get:9 http://mirrordirector.raspbian.org/raspbian/ jessie/main libmailutils4 armhf 1:2.99.98-2 [701 kB]
Get:10 http://mirrordirector.raspbian.org/raspbian/ jessie/main mailutils armhf 1:2.99.98-2 [572 kB]
Fetched 4,959 kB in 6s (819 kB/s)
Selecting previously unselected package libgc1c2:armhf.
(Reading database ... 33417 files and directories currently installed.)
Preparing to unpack .../libgc1c2_1%3a7.2d-6.4_armhf.deb ...
Unpacking libgc1c2:armhf (1:7.2d-6.4) ...
Selecting previously unselected package libunistring0:armhf.
Preparing to unpack .../libunistring0_0.9.3-5.2_armhf.deb ...
Unpacking libunistring0:armhf (0.9.3-5.2) ...
Selecting previously unselected package guile-2.0-libs:armhf.
Preparing to unpack .../guile-2.0-libs_2.0.11+1-9_armhf.deb ...
Unpacking guile-2.0-libs:armhf (2.0.11+1-9) ...
Selecting previously unselected package liblzo2-2:armhf.
Preparing to unpack .../liblzo2-2_2.08-1.2_armhf.deb ...
Unpacking liblzo2-2:armhf (2.08-1.2) ...
Selecting previously unselected package libkyotocabinet16:armhf.
Preparing to unpack .../libkyotocabinet16_1.2.76-4_armhf.deb ...
Unpacking libkyotocabinet16:armhf (1.2.76-4) ...
Selecting previously unselected package mailutils-common.
Preparing to unpack .../mailutils-common_1%3a2.99.98-2_all.deb ...
Unpacking mailutils-common (1:2.99.98-2) ...
Selecting previously unselected package libntlm0:armhf.
Preparing to unpack .../libntlm0_1.4-3_armhf.deb ...
Unpacking libntlm0:armhf (1.4-3) ...
Selecting previously unselected package libgsasl7.
Preparing to unpack .../libgsasl7_1.8.0-6_armhf.deb ...
Unpacking libgsasl7 (1.8.0-6) ...
Selecting previously unselected package libmailutils4:armhf.
Preparing to unpack .../libmailutils4_1%3a2.99.98-2_armhf.deb ...
Unpacking libmailutils4:armhf (1:2.99.98-2) ...
Selecting previously unselected package mailutils.
Preparing to unpack .../mailutils_1%3a2.99.98-2_armhf.deb ...
Unpacking mailutils (1:2.99.98-2) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up libgc1c2:armhf (1:7.2d-6.4) ...
Setting up libunistring0:armhf (0.9.3-5.2) ...
Setting up guile-2.0-libs:armhf (2.0.11+1-9) ...
Setting up liblzo2-2:armhf (2.08-1.2) ...
Setting up libkyotocabinet16:armhf (1.2.76-4) ...
Setting up mailutils-common (1:2.99.98-2) ...
Setting up libntlm0:armhf (1.4-3) ...
Setting up libgsasl7 (1.8.0-6) ...
Setting up libmailutils4:armhf (1:2.99.98-2) ...
Setting up mailutils (1:2.99.98-2) ...
update-alternatives: using /usr/bin/frm.mailutils to provide /usr/bin/frm (frm) in auto mode
update-alternatives: using /usr/bin/from.mailutils to provide /usr/bin/from (from) in auto mode
update-alternatives: using /usr/bin/messages.mailutils to provide /usr/bin/messages (messages) in auto mode
update-alternatives: using /usr/bin/movemail.mailutils to provide /usr/bin/movemail (movemail) in auto mode
update-alternatives: using /usr/bin/readmsg.mailutils to provide /usr/bin/readmsg (readmsg) in auto mode
update-alternatives: using /usr/bin/dotlock.mailutils to provide /usr/bin/dotlock (dotlock) in auto mode
update-alternatives: using /usr/bin/mail.mailutils to provide /usr/bin/mailx (mailx) in auto mode
Processing triggers for libc-bin (2.19-18+deb8u1) ...


OK, ensuite j'installe mpack qui doit me permettre d'encoder et décoder d’éventuelles pièces jointes.

pi@gladys:~ $ sudo apt-get install -y mpack
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  inews
The following NEW packages will be installed:
  mpack
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 38.4 kB of archives.
After this operation, 104 kB of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main mpack armhf 1.6-8 [38.4 kB]
Fetched 38.4 kB in 13s (2,890 B/s)
Selecting previously unselected package mpack.
(Reading database ... 34213 files and directories currently installed.)
Preparing to unpack .../archives/mpack_1.6-8_armhf.deb ...
Unpacking mpack (1.6-8) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up mpack (1.6-8) ...


Maintenant je configure  /etc/ssmtp/ssmtp.conf

 pi@gladys:~ $ sudo nano /etc/ssmtp/ssmtp.conf






Je modifie comme suit en ajoutant les lignes de la fin du fichier en bordeau ci dessous:


#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=gladys

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES


mailhub=smtp.gmail.com:587
AuthUser=jeanphilippebernard.dupontdugard@gmail.com
AuthPass=azertyuiop123456789
useSTARTTLS=YES
useTLS=YES


OK, je test maintenant cette configuration








pi@gladys:~ $ echo "Test configuration Gladys" | mail -s "premier test de configuration mail" jeanphilippebernard.dupontdugard@gmail.com


Là après quelques secondes message d'erreur :

mail: cannot send message: Process exited with a non-zero status

Même pas peur, google est mon ami je copie ce message dans la barre de recherche google.

Après quelques recherches il apparait qu'il faut faire un tour sur https://www.google.com/settings/security/lesssecureapps et "activer"

Nouveau test :

pi@gladys:~ $ echo "Test configuration Gladys" | mail -s "premier test de configuration mail" jeanphilippebernard.dupontdugard@gmail.com

Le mail passe sans souci.

Maintenant que mon raspberry sait envoyer des mails sur internet les mails générés par le système peuvent être renvoyés sur ma boite perso.

Il suffit de modifier le fichier /etc/aliases.

Je le fait comme suit :

    # /etc/aliases
    www: root
    root: dupontdugard@gmail.com

Les mails générés vers le user www seront redirigés vers le user root qui enverra les emails vers votre adresse externe Gmail.

Pour finir pour envoyer un mail avec pièce jointe :

mpack -s testpiècejointe piècejointe.ext jeanphilippebernard.dupontdugard@gmail.com


Bien sur pensez à remplacer  "tespiècejointe" par votre sujet "piècejointe.txt" par le nom de votre fichier à joindre et l'adresse mail par celle de votre destinataire

Source :
http://www.pihomeserver.fr/2015/08/13/envoyer-un-email-depuis-votre-raspberry-pi/

Quelques liens peut être utiles pour aller plus loin :

http://www.commentcamarche.net/faq/893-parametres-de-serveurs-pop-imap-et-smtp-des-principaux-fai
http://www.serversmtp.com/fr/liste-serveur-smtp


Aucun commentaire:

Enregistrer un commentaire