A tutorial that uses Python to sign in to Gmail and send Gmail messages

Source: Internet
Author: User
Tags mail code
This quick article describes using Gmail as your e-mail server to send e-mail via Python's built-in SMTP library. It's not complicated, I promise.

Here's how to sign in 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)

Here's how to send a message in Python:

headers = "\ r \ n". Join (["From:" + Gmail_username,            "Subject:" + Email_subject            "to:" + recipient,            "Mime-versi on: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 mastery of Python, this can be a fairly small or quite long code.


For me, the first time I took a program to send an email, as I saw the scene in The matrix, it was like the first time I built a website on Godaddy, or as I first used Joptionpane's doubts. This is an implementation method (a simple method that makes implementing a similar application very simple), although I've seen a lot of e-mail code before, but it still leaves a deep impression on me.


And, despite the simplicity of the code above, I spent the first time trying my best to write the code out for two hours. So I hope to 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.