Send email to multiple recipients in Python (Python 3)

I was addicted to specifying multiple destinations for TO, CC, BCC, so make a note Since it is written that the method called send_message can call sendmail () with the message object as an argument, I tried using it. I think it's very convenient.

http://docs.python.jp/3.3/library/smtplib.html



#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text      import MIMEText

def send_msg(
    from_addr, to_addrs = [], cc_addrs = [], bcc_addrs = [],
    subject = "", body_html = "", body_text = ""):
    msg  = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From']    = from_addr
    msg['To']      = ",".join(to_addrs)

    if cc_addrs !=[]:
        msg['Cc'] = ",".join(cc_addrs)
    if bcc_addrs !=[]:
        msg['Bcc'] = ",".join(bcc_addrs)

    cset = 'utf-8'

    if body_text != "":
        attachment_text = MIMEText(body_text, 'plain', cset)
        msg.attach(attachment_text)

    if body_html != "":
        attachment_html = MIMEText(body_html, 'html' , cset)
        msg.attach(attachment_html)

    smtp_con = smtplib.SMTP('localhost',25)
    smtp_con.set_debuglevel(True)
    smtp_con.send_message(msg = msg)
    smtp_con.close()

Recommended Posts

Send email to multiple recipients in Python (Python 3)
Send email in Python (Outlook)
[Python] Send email
[Python] Send email
HTML email with image to send with python
Send email with Python
Send Gmail in Python
Send an email to Spushi's address with python
Send messages to Skype and Chatwork in Python
Send push notifications to iOS apps in Python
Send an email with Excel attached in Python
Multiple regression expressions in Python
Send Japanese email with Python3
To flush stdout in Python
How to write string concatenation in multiple lines in Python
Login to website in Python
Speech to speech in python [text to speech]
Avoid multiple loops in Python
How to retrieve multiple arrays using slice in python.
How to develop in Python
Prohibit multiple launches in python
To automatically send an email with an attachment using the Gmail API in Python
Post to Slack in Python
How to slice a block multiple array from a multiple array in Python
[R] [Python] Memo to read multiple csv files in multiple zip files
A simple way to avoid multiple for loops in Python
How to define multiple variables in a python for statement
Extract multiple list duplicates in Python
[Python] Show multiple windows in Tkinter
[Python] How to do PCA in Python
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
[Automation] Send Outlook email with Python
In the python command python points to python3.8
Try to calculate Trace in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Statistical test (multiple test) in Python: scikit_posthocs
Delete multiple elements in python list
[Python] Send an email with outlook
How to handle Japanese in Python
An alternative to `pause` in Python
[Python] Create API to send Gmail
Send email via gmail with Python 3.4.3.
How to delete multiple specified positions (indexes) in a Python list
Handle multiple python versions in one jupyter
When specifying multiple keys in python sort
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
How to access environment variables in Python
Method to build Python environment in Xcode 6
How to do R chartr () in Python
Pin current directory to script directory in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python