Use Python to log on to Gmail and send a Gmail mail tutorial, pythongmail

Source: Internet
Author: User
Tags mail code gmail mail

Use Python to log on to Gmail and send a Gmail mail tutorial, pythongmail

This article describes how to use Gmail as your e-mail server to send emails through the built-in SMTP library of Python. 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.

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.