メール送信

メモ


Gmail送信

メモ

Gmail 送信 (Python 3.2 以上)
import smtplib
from email.message import EmailMessage

GMAIL_SERVER = "smtp.gmail.com"
GMAIL_PORT = 587
GMAIL_PORT_SSL = 465

# メッセージ作成 Python 3.2 以上
# to・cc・bcc:文字列 (複数:カンマ区切り)・リスト
def create_msg_32(subject, body, from_, to, cc=None, bcc=None):
    msg = EmailMessage()
    msg['Subject'] = subject
    msg['From'] = from_
    msg['To'] = to
    msg['Cc'] = cc
    msg['Bcc'] = bcc
    msg.set_content(body)
    return msg

# Gmail 送信 (TLS) Python 3.2 以上
def send_gmail_tls_32(user, password, msg, debug=False):
    print("開始:Gmail 送信 (TLS)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    with smtplib.SMTP(GMAIL_SERVER, GMAIL_PORT) as smtp:
        # デバッグ出力レベル設定
        smtp.set_debuglevel(debug)
        # TLS 開始
        smtp.ehlo()  # 省略可
        smtp.starttls()
        # ログイン
        smtp.ehlo()  # 省略可
        smtp.login(user, password)
        # メール送信
        result = smtp.send_message(msg)
        # with文により QUITコマンド自動発行
    print("終了:Gmail 送信 (TLS)")
    return result

# Gmail 送信 (SSL) Python 3.2 以上
def send_gmail_ssl_32(user, password, msg, debug=False):
    print("開始:Gmail 送信 (SSL)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    with smtplib.SMTP_SSL(GMAIL_SERVER, GMAIL_PORT_SSL) as smtp:
        # デバッグ出力レベル設定
        smtp.set_debuglevel(debug)
        # ログイン
        smtp.ehlo()  # 省略可
        smtp.login(user, password)
        # メール送信
        result = smtp.send_message(msg)
        # with文により QUITコマンド自動発行
    print("終了:Gmail 送信 (SSL)")
    return result

if __name__ == "__main__":
    gmail_user = "~@gmail.com"
    gmail_password = "~"
    # TO・CC・BCC:文字列 (複数:カンマ区切り)・リスト
    to = ”~”
    cc = [ ”~”, ”~” ]
    bcc = "~, ~"
    body = "本文1\n本文2\n本文3"

    # Gmail 送信 (TLS)
    subject = "件名:Gmail (TLS) by Python 3.2 以上"
    msg = create_msg_32(subject, body, gmail_user, to, cc, bcc)
    send_gmail_tls_32(gmail_user, gmail_password, msg)

    # Gmail 送信 (SSL)
    subject = "件名:Gmail (SSL) by Python 3.2 以上"
    msg = create_msg_32(subject, body, gmail_user, to, cc, bcc)
    send_gmail_ssl_32(gmail_user, gmail_password, msg)
【出力例】
開始:Gmail 送信 (TLS)
SUBJECT: 件名:Gmail (TLS) by Python 3.2 以上
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Gmail 送信 (TLS)
開始:Gmail 送信 (SSL)
SUBJECT: 件名:Gmail (SSL) by Python 3.2 以上
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Gmail 送信 (SSL)
Gmail 送信 (Python 3.2 未満)
import smtplib
from email.mime.text import MIMEText
import copy

GMAIL_SERVER = "smtp.gmail.com"
GMAIL_PORT = 587
GMAIL_PORT_SSL = 465

# メッセージ作成 Python 3.2 未満
# to・cc・bcc:文字列 (複数:カンマ区切り)
def create_msg_31(subject, body, from_, to, cc=None, bcc=None):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_
    msg['To'] = to
    msg['Cc'] = cc
    msg['Bcc'] = bcc
    return msg
    
# Gmail 送信 (TLS) Python 3.2 未満
def send_gmail_tls_31(user, password, msg, debug=False):
    print("開始:Gmail 送信 (TLS)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    # 接続
    smtp = smtplib.SMTP(GMAIL_SERVER, GMAIL_PORT)
    # デバッグ出力レベル設定
    smtp.set_debuglevel(debug)
    # TLS 開始
    smtp.ehlo()  # 省略可
    smtp.starttls()
    # ログイン
    smtp.ehlo()  # 省略可
    smtp.login(user, password)
    # メール送信
    list_to = msg['To'].split(",")
    if msg['Cc'] is not None:
        list_to += msg['Cc'].split(",")
    if msg['Bcc'] is not None:
        list_to += msg['Bcc'].split(",")
    msg_copy = copy.copy(msg)
    del msg_copy['Bcc']
    result = smtp.sendmail(msg['From'], list_to, msg_copy.as_string())
    # 切断
    smtp.quit()
    print("終了:Gmail 送信 (TLS)")
    return result

# Gmail 送信 (SSL) Python 3.2 未満
def send_gmail_ssl_31(user, password, msg, debug=False):
    print("開始:Gmail 送信 (SSL)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    # 接続
    smtp = smtplib.SMTP_SSL(GMAIL_SERVER, GMAIL_PORT_SSL)
    # デバッグ出力レベル設定
    smtp.set_debuglevel(debug)
    # ログイン
    smtp.ehlo()  # 省略可
    smtp.login(user, password)
    # 送信先編集
    list_to = msg['To'].split(",")
    if msg['Cc'] is not None:
        list_to += msg['Cc'].split(",")
    if msg['Bcc'] is not None:
        list_to += msg['Bcc'].split(",")
    msg_copy = copy.copy(msg)
    del msg_copy['Bcc']
    # メール送信
    result = smtp.sendmail(msg['From'], list_to, msg_copy.as_string())
    # 切断
    smtp.quit()
    print("終了:Gmail 送信 (SSL)")
    return result

if __name__ == "__main__":
    gmail_user = "~@gmail.com"
    gmail_password = "~"
    to = "~"
    cc = "~,~"
    bcc = "~,~"
    body = "本文1\n本文2\n本文3"

    # Gmail 送信 (TLS)
    subject = "件名:Gmail (TLS) by Python 3.2 未満"
    msg = create_msg_31(subject, body, gmail_user, to, cc, bcc)
    send_gmail_tls_31(gmail_user, gmail_password, msg)

    # Gmail 送信 (SSL)
    subject = "件名:Gmail (SSL) by Python 3.2 未満"
    msg = create_msg_31(subject, body, gmail_user, to, cc, bcc)
    send_gmail_ssl_31(gmail_user, gmail_password, msg)
【出力例】
開始:Gmail 送信 (TLS)
SUBJECT: 件名:Gmail (TLS) by Python 3.2 未満
FROM: ~
TO: ~
CC: ~,~
BCC: ~,~
終了:Gmail 送信 (TLS)
開始:Gmail 送信 (SSL)
SUBJECT: 件名:Gmail (SSL) by Python 3.2 未満
FROM: ~
TO: ~
CC: ~,~
BCC: ~,~
終了:Gmail 送信 (SSL)

Yahoo!メール送信

メモ

Yahoo! メール 送信 (Python 3.2 以上)
import smtplib
from email.message import EmailMessage

YAHOO_SERVER = "smtp.mail.yahoo.co.jp"
YAHOO_PORT = 587
YAHOO_PORT_SSL = 465

# メッセージ作成 Python 3.2 以上
# to・cc・bcc:文字列 (複数:カンマ区切り)・リスト
def create_msg_32(subject, body, from_, to, cc=None, bcc=None):
    msg = EmailMessage()
    msg['Subject'] = subject
    msg['From'] = from_
    msg['To'] = to
    msg['Cc'] = cc
    msg['Bcc'] = bcc
    msg.set_content(body)
    return msg

# Yahoo!メール 送信 (SSL) Python 3.2 以上
def send_yahoo_ssl_32(user, password, msg, debug=False):
    print("開始:Yahoo!メール 送信 (SSL)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    with smtplib.SMTP_SSL(YAHOO_SERVER, YAHOO_PORT_SSL) as smtp:
        # デバッグ出力レベル設定
        smtp.set_debuglevel(debug)
        # ログイン
        smtp.ehlo()  # 省略可
        smtp.login(user, password)
        # メール送信
        result = smtp.send_message(msg)
        # with文により QUITコマンド自動発行
    print("終了:Yahoo!メール 送信 (SSL)")
    return result

# Yahoo!メール 送信 Python 3.2 以上
def send_yahoo_32(user, password, msg, debug=False):
    print("開始:Yahoo!メール 送信")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    with smtplib.SMTP(YAHOO_SERVER, YAHOO_PORT) as smtp:
        # デバッグ出力レベル設定
        smtp.set_debuglevel(debug)
        # ログイン
        smtp.ehlo()  # 省略可
        smtp.login(user, password)
        # メール送信
        result = smtp.send_message(msg)
        # with文により QUITコマンド自動発行
    print("終了:Yahoo!メール 送信")
    return result

if __name__ == "__main__":
    yahoo_user = "~"
    yahoo_mail = "~@yahoo.co.jp"
    yahoo_password = "~"
    # TO・CC・BCC:文字列 (複数:カンマ区切り)・リスト
    to = "~"
    cc = [ "~", "~" ]
    bcc = "~,~"
    body = "本文1\n本文2\n本文3"

    # Yahoo!メール 送信 (SSL) 推奨
    subject = "件名:Yahoo!メール (SSL) by Python 3.2 以上"
    msg = create_msg_32(subject, body, yahoo_mail, to, cc, bcc)
    send_yahoo_ssl_32(yahoo_user, yahoo_password, msg)

    # Yahoo!メール 送信
    subject = "件名:Yahoo!メール by Python 3.2 以上"
    msg = create_msg_32(subject, body, yahoo_mail, to, cc, bcc)
    send_yahoo_32(yahoo_user, yahoo_password, msg)
【出力例】
開始:Yahoo!メール 送信 (SSL)
SUBJECT: 件名:Yahoo!メール (SSL) by Python 3.2 以上
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Yahoo!メール 送信 (SSL)
開始:Yahoo!メール 送信
SUBJECT: 件名:Yahoo!メール by Python 3.2 以上
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Yahoo!メール 送信
Yahoo! メール 送信 (Python 3.2 未満)
import smtplib
from email.mime.text import MIMEText
import copy

YAHOO_SERVER = "smtp.mail.yahoo.co.jp"
YAHOO_PORT = 587
YAHOO_PORT_SSL = 465

# メッセージ作成 Python 3.2 未満
# to・cc・bcc:文字列 (複数:カンマ区切り)
def create_msg_31(subject, body, from_, to, cc=None, bcc=None):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_
    msg['To'] = to
    msg['Cc'] = cc
    msg['Bcc'] = bcc
    return msg
    
# Yahoo!メール 送信 (SSL) Python 3.2 未満
def send_yahoo_ssl_31(user, password, msg, debug=False):
    print("開始:Yahoo!メール 送信 (SSL)")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    # 接続
    smtp = smtplib.SMTP_SSL(YAHOO_SERVER, YAHOO_PORT_SSL)
    # デバッグ出力レベル設定
    smtp.set_debuglevel(debug)
    # ログイン
    smtp.ehlo()  # 省略可
    smtp.login(user, password)
    # メール送信
    list_to = msg['To'].split(",")
    if msg['Cc'] is not None:
        list_to += msg['Cc'].split(",")
    if msg['Bcc'] is not None:
        list_to += msg['Bcc'].split(",")
    msg_copy = copy.copy(msg)
    del msg_copy['Bcc']
    result = smtp.sendmail(msg['From'], list_to, msg_copy.as_string())
    # 切断
    smtp.quit()
    print("終了:Yahoo!メール 送信 (SSL)")
    return result

# Yahoo!メール 送信 Python 3.2 未満
def send_yahoo_31(user, password, msg, debug=False):
    print("開始:Yahoo!メール 送信")
    print("SUBJECT:", msg['Subject'])
    print("FROM:", msg['From'])
    print("TO:", msg['To'])
    print("CC:", msg['Cc'])
    print("BCC:", msg['Bcc'])
    # 接続
    smtp = smtplib.SMTP(YAHOO_SERVER, YAHOO_PORT)
    # デバッグ出力レベル設定
    smtp.set_debuglevel(debug)
    # ログイン
    smtp.ehlo()  # 省略可
    smtp.login(user, password)
    # 送信先編集
    list_to = msg['To'].split(",")
    if msg['Cc'] is not None:
        list_to += msg['Cc'].split(",")
    if msg['Bcc'] is not None:
        list_to += msg['Bcc'].split(",")
    msg_copy = copy.copy(msg)
    del msg_copy['Bcc']
    # メール送信
    result = smtp.sendmail(msg['From'], list_to, msg_copy.as_string())
    # 切断
    smtp.quit()
    print("終了:Yahoo!メール 送信")
    return result

if __name__ == "__main__":
    yahoo_user = "~"
    yahoo_mail = "~@yahoo.co.jp"
    yahoo_password = "~"
    # TO・CC・BCC:文字列 (複数:カンマ区切り)
    to = yahoo_mail
    cc = "~, ~"
    bcc = "~, ~"
    body = "本文1\n本文2\n本文3"

    # Yahoo!メール 送信 (SSL) 推奨
    subject = "件名:Yahoo!メール (SSL) by Python 3.2 未満"
    msg = create_msg_31(subject, body, yahoo_mail, to, cc, bcc)
    send_yahoo_ssl_31(yahoo_user, yahoo_password, msg)

    # Yahoo!メール 送信
    subject = "件名:Yahoo!メール by Python 3.2 未満"
    msg = create_msg_31(subject, body, yahoo_mail, to, cc, bcc)
    send_yahoo_31(yahoo_user, yahoo_password, msg)
【出力例】
開始:Yahoo!メール 送信 (SSL)
SUBJECT: 件名:Yahoo!メール (SSL) by Python 3.2 未満
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Yahoo!メール 送信 (SSL)
開始:Yahoo!メール 送信
SUBJECT: 件名:Yahoo!メール by Python 3.2 未満
FROM: ~
TO: ~
CC: ~, ~
BCC: ~, ~
終了:Yahoo!メール 送信