1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

大量にyahooメールを送信する

Last updated at Posted at 2021-08-23

下記サイトを参考に大量にメールを送信するプログラムにしました。
アカウントで1日最大500通送付できます(ヤフーの仕様でそれ以上はできない)
https://cercopes-z.com/Python/tips-mail-py.html#yahoo-36

send_yahoomail_jp_3_6.pyとyahoomail.pyを同じフォルダに入れます。
yahoomail.pyを実行すればOKです。

基本yahoomail.pyのヤフーアドレスとパスワード、メールの内容を編集するだけで使えます。

セーフティアドレスを使う場合send_yahoomail_jp_3_6.pyのコメントアウトしているところをいじってください。

send_yahoomail_jp_3_6.py
# send_yahoomail_jp_3_6.py
# Yahoo!メール 送信 (Python 3.6 以上)

import mimetypes
import os
import smtplib
from email.message import EmailMessage

# Yahoo!メール 送信 クラス
class YahooMailJp:
    # コンストラクタ
    def __init__(self, user, password):
        self._user = user
        self._password = password


    # メール送信 (SSL)
    def send(self, subject, body, to, cc=None, bcc=None, *, files=[], debug=0):
        YAHOO_SMTP_SERVER = "smtp.mail.yahoo.co.jp"
        YAHOO_PORT_SSL = 465
        YAHOO_MAIL_SUFFIX = "@yahoo.co.jp"
        DEBUG_TRACE = 8

        trace = True if (debug == DEBUG_TRACE) else False
        if trace:
            print("開始:Yahoo!メール 送信 (SSL)")

        # メール作成
        msg = EmailMessage()

        # ヘッダー情報 設定
        msg['Subject'] = subject
        msg['From'] = self._user + YAHOO_MAIL_SUFFIX
        #msg['From'] = "セーフティアドレスを使う場合はここにアドレスを入力して、上をコメントアウト"
        msg['To'] = to
        if cc is not None:
            msg['Cc'] = cc
        if bcc is not None: 
            msg['Bcc'] = bcc
        if trace:
            print("Subject:", msg['Subject'])
            print("From:", msg['From'])
            print("To:", msg['To'])
            print("Cc:", msg['Cc'])
            print("Bcc:", msg['Bcc'])

        # 本文設定
        html = body.startswith("<html>")
        msg.set_content(body)
        if html:
            msg.set_type("text/html")

        # 添付ファイル追加
        if trace:
            print("FILES:", len(files))
        for file in files:
            ctype, encoding = mimetypes.guess_type(file)
            if (ctype is None) or (encoding is not None):
                ctype = 'application/octet-stream'
            maintype, subtype = ctype.split('/', 1)
            with open(file, 'rb') as fp:
                msg.add_attachment(
                        fp.read(),
                        maintype=maintype,
                        subtype=subtype,
                        filename=os.path.basename(file))
                if trace:
                    print(f"  {file} ({maintype}/{subtype})")

        # メール送信 (SSL)
        with smtplib.SMTP_SSL(YAHOO_SMTP_SERVER, YAHOO_PORT_SSL) as smtp:
            # デバッグ出力レベル設定
            if not trace:
                smtp.set_debuglevel(debug)
            # ログイン
            smtp.ehlo()
            if trace:
                print("Support:", smtp.esmtp_features)
            smtp.login(self._user, self._password)
            # メール送信
            result = smtp.send_message(msg)
            # with文により QUITコマンド自動発行

        if trace:
            if len(result) != 0:
                print(result)
            print("終了:Yahoo!メール 送信 (SSL)\n")
        return result
yahoomail.py
from send_yahoomail_jp_3_6 import YahooMailJp
from time import sleep
import random, string

yahoo_user = "ヤフーアカウント名"  # @yahoo.co.jp 省略
yahoo_password = "パスワード"

# Yahoo!メール 送信 オブジェクト生成
yahoo_mail = YahooMailJp(yahoo_user, yahoo_password)

# プレーンテキスト
subject = "件名:amazonギフト券のコードです "
body = "本文1\n本文2\n本文3"
to = " "
yahoo_mail.send(subject, body, to)
"""
# HTMLテキスト・TO (複数)
subject = "件名:Yahoo!メール HTMLテキスト TO (複数)"
body = "<html>本文1<br><b style='color: red;'>本文2</b><br>本文3</html>"
to = ("chocolate.3.planette@gmail.com", "chocolate.3.planette@gmail.com")
yahoo_mail.send(subject, body, to)

# プレーンテキスト・添付ファイル
subject = "件名:amazonギフト券のコードです "
body = "本文"
to = "chocolate.3.planette@gmail.com"
files = ("./mail/a.jpg", "./mail/b.jpg", "./mail/c.jpg")
"""
#仮のギフト券番号
def randomname(n):
   randlst = [random.choice(string.ascii_letters + string.digits) for i in range(n)]
   return ''.join(randlst)

#100通メール送信
for num in range(100):
    
   # body = randomname(4)+"-"+randomname(5)+"-"+randomname(5)
    yahoo_mail.send(subject, body, to, files=files)
    print(num)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?