Python : envoi de mail

Avec Gmail, désactivez l’accès sécurisé : https://myaccount.google.com/lesssecureapps

Envoi d’un texte simple :

import smtplib, ssl

    
port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "zzzzs@gmail.com"
receiver_email = "xxxxs@gmail.com"
password = "yyyyyy"
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

Envoi d’un texte avec HTML :

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender_email = "xxxx@gmail.com"
receiver_email = "yyyyys@gmail.com"
password = "zzzzz"

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\
<html>
  <body>
    <p>Hi,<br>
       How are you?<br>
       <a href="http://www.realpython.com">Real Python</a> 
       has many great tutorials.
    </p>
  </body>
</html>
"""

# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")

# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)

# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )

Pour lancer un script Python en tâche de fond ajouter & : ~$ python script.py &

Stopper le processus, 1°) lister les processus : ps -aux | grep python

2°) faire un kill du processus : kill <<numéro du processus>>

Pour créer un environnement virtuel <<Nom env virtuel>> :

cyrgui@CyrguiUbuntu:~$ python3 -m venv <<Nom env virtuel>>
cyrgui@CyrguiUbuntu:~$ source <<Nom env virtuel>>bin/activate
(<<Nom env virtuel>>) cyrgui@CyrguiUbuntu:~$ python script.py &

Utilisation d’API avec Requests :

Listes d’API : https://any-api.com/

Exemple la météo : https://weatherstack.com/documentation

Références : https://www.tutorialspoint.com/python3/python_sending_email.htm

https://realpython.com/python-send-email/#option-1-setting-up-a-gmail-account-for-development

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :