Send email via gmail with Python 3.4.3.

It's just a personal note.


#!/usr/bin/python

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate

class Gmail:
    """
    Gmail with smtp client
    """
    def __init__(self, login_addr, passwd, encoding='utf-8'):
        self._encoding = encoding
        self._login_addr = login_addr
        self._passwd = passwd

    def send(self, to_addr, from_addr, subject, body):
        """
        Send a mail via gmail
        """
        msg = self._format_email(to_addr, from_addr, subject, body)
        with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            stmp.ehlo()
            stmp.starttls()
            stmp.ehlo()
            stmp.login(self._login_addr, self._passwd)
            stmp.sendmail(from_addr, to_addr, msg.as_string())

    def _format_email(self, to_addr, from_addr, subject, body):
        msg = MIMEText(body, 'html', self._encoding)
        msg['Subject'] = Header(subject, self._encoding)
        msg['From'] = from_addr
        msg['To'] = to_addr
        msg['Date'] = formatdate()

        return msg

    def bulk_send(self, emails):
        """
        Send multiple mails at one time via gmail
        """
        with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()
            smtp.login(self._login_addr, self._passwd)
            for email in emails:
                msg = self._format_email(email['to_addr'], email['from_addr'],
                                         email['subject'], email['body'])
                smtp.sendmail(email['from_addr'], email['to_addr'],
                              msg.as_string())


if __name__ == "__main__":
    gmail = Gmail('[email protected]', 'foo')
    gmail.send('[email protected]', '[email protected]', 'test', 'test')
    addr = '[email protected]'
    mails = [{'to_addr': addr, 'from_addr': addr, 'subject': 'test', 'body': 'test'}] * 10
    gmail.bulk_send(mails)




Recommended Posts

Send email via gmail with Python 3.4.3.
Send email with Python
Send Japanese email with Python3
Send using Python with Gmail
[Python] Send email
[Python] Send an email with outlook
[Python] Send an email from gmail with two-step verification set
Send an email with Amazon SES + Python
Send email with Django
Validate E-Mail with Python
Send Gmail in Python
Send an email with Excel attached in Python
Send email in Python (Outlook)
Send and receive Gmail via the Gmail API using Python
To automatically send an email with an attachment using the Gmail API in Python
[Python] Send gmail with python: Send one by one with multiple image files attached
Easy email sending with haste python3
Note: Send an email with Django
Send multipart / form-data with python requests
Easily send emails with Gmail with Django
Send image with python, save with php
[Python] Create API to send Gmail
Send newsletters all at once with Gmail
Send email to multiple recipients in Python (Python 3)
I tried sending an email with python.
Send files via bastion server with scp
Python 3.6 email library
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Send an email to a specific email address with python without SMTP settings
Send email with SES in Python and short message with SMS on SNS
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
I tried to send a registration completion email from Gmail with django.
Zundokokiyoshi with python
Send and receive binary data via serial communication with python3 (on mac)
Excel with Python
Microcomputer with Python
Cast with python
Send a message to LINE with Python (LINE Notify)
Email attachments using your gmail account in python.
I want to email from Gmail using Python.
Sample to send slack notification with python lambda
Get Gmail subject and body with Python and Gmail API
I tried sending an email with SendGrid + Python
I want to send Gmail with Python, but I can't because of an error
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2