A tutorial on using Python to log in to Gmail and send Gmail messages _python

Source: Internet
Author: User
Tags in python

This quick article introduces the use of Gmail as your e-mail server, through Python's built-in SMTP library to send e-mail. It's not complicated, I promise.

Here's how to log in to Gmail in Python:

Import smtplib
 
# below code never changes, though obviously those variables values.
Session = Smtplib. SMTP (' smtp.gmail.com ', 587)
Session.ehlo ()
session.starttls ()
session.login (Gmail_username, Gmail_ PASSWORD)

Here's how to send a message in Python:

headers = "\ r \ n". Join (["From:" + Gmail_username,
            "Subject:" + Email_subject
            "to:" + recipient,
            "mime-vers ion:1.0 ",
            " content-type:text/html "])
 
# body_of_email can be plaintext or html!          
Content = headers + "\r\n\r\n" + body_of_email
session.sendmail (gmail_username, recipient, content)

Depending on your mastery of Python, this could be a fairly small or fairly long code.


For me, the first time I took a program to send an email, as I saw the scene in The matrix, like the first time I built a website on a Godaddy, or the first time I used a joptionpane puzzle. This is an implementation approach (a simple way to make a similar application very simple), although I've seen a lot of email code before, but it still impresses me.


In addition, although the above code is simple, but the first time I did my best, it took two hours to write the code. So I hope I can help some other people.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.