Email attachments using your gmail account in python.

Things to prepare

--Create a Gmail account --Turn on "Allow insecure apps"

code

from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from email.utils import formatdate
import smtplib
import ssl

if __name__ == '__main__':
    #Gmail account information(★ Setting required ★)
    mail_username = 'Created Gmail account name'
    mail_password = 'Password for the Gmail account you created'

    #Email information(★ Change as needed ★)
    body = 'This is the text.'
    subject = 'This is the title.'
    to_addrs = ['[email protected]', '[email protected]'] #Address list sent
    attach_files = ['aaa.zip'] #Set the local file name you want to attach
    
    _msg = MIMEMultipart()
    _msg['From'] = mail_username
    _msg['To'] = "; ".join(to_addrs)
    _msg['Subject'] = subject
    _msg['Date'] = formatdate(timeval=None, localtime=True)

    #Add text
    _msg.attach(MIMEText(body, "plain"))

    #Add attachment
    for filename in attach_files:
        with open(filename, 'rb') as _f:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(_f.read())

        # base64 encode
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename= {}'.format(filename))
        
        _msg.attach(part)

    #Send over secure SSL connection
    context = ssl.create_default_context()

    _smtp = smtplib.SMTP_SSL(host='smtp.gmail.com', port=465, timeout=10, context=context)
    _smtp.login(user=mail_username, password=mail_password)
    _smtp.sendmail(mail_username, to_addrs, _msg.as_string())
    _smtp.close()

Recommended Posts

Email attachments using your gmail account in python.
I want to email from Gmail using Python.
Draft Gmail in Python
Extract email attachments received in Thunderbird with Python
Send Gmail in Python
To automatically send an email with an attachment using the Gmail API in Python
Create your first GDSII file in Python using gdspy
Send email in Python (Outlook)
Translate using googletrans in Python
Using Python mode in Processing
Send using Python with Gmail
GUI programming in Python using Appjar
Precautions when using pit in Python
[Python] logging in your own module
Try using LevelDB in Python (plyvel)
Create Gmail in Python without API
Using global variables in python functions
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
Send email via gmail with Python 3.4.3.
GUI creation in python using tkinter 2
Try to log in to Netflix automatically using python on your PC
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Create your own Linux commands in Python
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
[LLDB] Create your own command in Python
Easily use your own functions in Python
Send email to multiple recipients in Python (Python 3)
[FX] Hit oanda-API in Python using Docker
Get mail using Gmail API in Java
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Log in to Slack using requests in Python
Get Youtube data in Python using Youtube Data API
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Create a record with attachments in KINTONE using the Python requests module
Disease classification in Random Forest using Python
Try text mining your diary in Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
Get your own IP address in Python
Meaning of using DI framework in Python
Send an email directly by pulling an MX record from your email address in Python