15-03-2019
This commit is contained in:
163
docs/Raspberry/send_mail.md
Normal file
163
docs/Raspberry/send_mail.md
Normal file
@@ -0,0 +1,163 @@
|
||||
#### Configurer un compte Gmail:
|
||||
|
||||
Sécurité -> Allow less secure apps
|
||||
|
||||
#### Installer SSMTP
|
||||
|
||||
```bash
|
||||
$ sudo apt-get install ssmtp
|
||||
Installer mpack (pour envoyer des PJ)
|
||||
$ sudo apt-get install mpack
|
||||
```
|
||||
|
||||
#### Configurer SSMTP
|
||||
|
||||
https://wiki.archlinux.org/index.php/SSMTP
|
||||
|
||||
```bash
|
||||
$ sudo nano /etc/ssmtp/ssmtp.conf
|
||||
```
|
||||
|
||||
et ajouter ces lignes:
|
||||
|
||||
```bash
|
||||
root=username@gmail.com
|
||||
mailhub=smtp.gmail.com:465
|
||||
rewriteDomain=gmail.com
|
||||
AuthUser=username
|
||||
AuthPass=password
|
||||
FromLineOverride=YES
|
||||
UseTLS=YES
|
||||
```
|
||||
|
||||
```bash
|
||||
$ sudo nano /etc/ssmtp/revaliases
|
||||
```
|
||||
|
||||
#### Vérifier le log:
|
||||
|
||||
```bash
|
||||
$ tail -f /var/log/mail.log
|
||||
$ tail -f /var/log/syslog
|
||||
```
|
||||
|
||||
#### Sécuriser ssmtp.conf
|
||||
|
||||
Ajouter le groupe ssmtp
|
||||
|
||||
```bash
|
||||
$ sudo groupadd ssmtp
|
||||
```
|
||||
|
||||
Donner le groupe ssmtp au fichier ssmtp.conf
|
||||
|
||||
```bash
|
||||
$ sudo chown :ssmtp /etc/ssmtp/ssmtp.conf
|
||||
```
|
||||
|
||||
Donner le groupe ssmtp au fichier à l'exécutable ssmtp
|
||||
|
||||
```bash
|
||||
$ sudo chown :ssmtp /usr/sbin/ssmtp
|
||||
```
|
||||
|
||||
S'assurer que juste root et le group ssmtp ont accès au fichier ssmtp.conf
|
||||
|
||||
```bash
|
||||
$ sudo chmod 640 /etc/ssmtp/ssmtp.conf
|
||||
```
|
||||
|
||||
Mettre le bit SGID à l'exécutable ssmtp
|
||||
|
||||
```bash
|
||||
$ sudo chmod g+s /usr/sbin/ssmtp
|
||||
```
|
||||
|
||||
Envoyer un email:
|
||||
|
||||
```bash
|
||||
$ echo 'Test text' | mail -s 'Test Mail' username@domaine.com
|
||||
$ ssmtp username@domaine.com
|
||||
subject: ceci est un test !
|
||||
hello world !
|
||||
<CTRL>+D pour envoyer
|
||||
```
|
||||
|
||||
```bash
|
||||
$ echo "Hello world email body" | mail -s "Test Subject" username@gmail.com
|
||||
```
|
||||
|
||||
Envoyer un fichier:
|
||||
|
||||
```bash
|
||||
$ mpack -s 'Test image en PJ' o.jpg username@domaine.com
|
||||
```
|
||||
|
||||
Envoyer un mail et une PJ:
|
||||
|
||||
```bash
|
||||
$ nano mail_template.txt
|
||||
Bonjour,
|
||||
|
||||
Ci-joint la photo demandée.
|
||||
|
||||
Cordialement.
|
||||
|
||||
$ mpack -s 'Test image en PJ' -d mail_template.txt image.jpg username@gmail.com
|
||||
```
|
||||
|
||||
Changer le from name = 'root'
|
||||
|
||||
```bash
|
||||
$ sudo nano /etc/passwd
|
||||
# Remplacer
|
||||
root:x:0:0:root:/root:/bin/bash
|
||||
# par
|
||||
root:x:0:0:RPi3:/root:/bin/bash
|
||||
```
|
||||
|
||||
|
||||
Envoyer un message avec PHP:
|
||||
|
||||
1. Editer le fichier de conf de PHP
|
||||
|
||||
```
|
||||
$ nano /etc/php/apache/php.ini
|
||||
```
|
||||
|
||||
2. Vérifier la ligne sendmail_path
|
||||
|
||||
```
|
||||
sendmail_path = /usr/sbin/sendmail -t -i
|
||||
```
|
||||
|
||||
3. Créer un fichier mailtest.php
|
||||
```php
|
||||
<?php
|
||||
$to = “targetperson@example.com”;
|
||||
$subject = “PHP Test mail”;
|
||||
$message = “This is a test email”;
|
||||
$from = “you@your.domain”;
|
||||
$headers = “From:” . $from;
|
||||
mail($to,$subject,$message,$headers);
|
||||
echo “Mail Sent.”;
|
||||
?>
|
||||
```
|
||||
|
||||
4. puis
|
||||
|
||||
```bash
|
||||
$ php mailtest.php
|
||||
```
|
||||
|
||||
|
||||
|
||||
Envoyer un message avec Python:
|
||||
|
||||
```bash
|
||||
import smtplib
|
||||
```
|
||||
|
||||
|
||||
|
||||
https://myhydropi.com/send-email-with-a-raspberry-pi-and-python
|
||||
Reference in New Issue
Block a user