This article mainly introduces how to use Python to log on to Gmail and send Gmail emails. using the Python SMTP library, the code is very simple, for more information about using Gmail as your e-mail server, use the built-in SMTP library of Python to send emails. It is not complicated, I promise.
The following describes how to log on to GMail in Python:
import smtplib # The below code never changes, though obviously those variables need values.session = smtplib.SMTP('smtp.gmail.com', 587)session.ehlo()session.starttls()session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
The following describes how to send emails in Python:
headers = "\r\n".join(["from: " + GMAIL_USERNAME, "subject: " + email_subject "to: " + recipient, "mime-version: 1.0", "content-type: text/html"]) # body_of_email can be plaintext or html! content = headers + "\r\n\r\n" + body_of_emailsession.sendmail(GMAIL_USERNAME, recipient, content)
Depending on your understanding of Python, this may be a fairly small or quite long piece of code.
For me, the first time I used a program to send an email, just like the moment I saw the scenario in the matrix, just like the first time I set up a website on Godaddy, or, for the first time I used JOptionPane. This is an implementation method (a simple method that makes it very simple to implement similar applications), although I have read a lot of mail code before, however, this still leaves a deep impression on me.
In addition, despite the simplicity of the above code, I did my best for the first time. it took more than two hours to write those codes. So I want to help others.