TransWikia.com

Python: smtplib: エラー「AttributeError: module 'smtplib' has no attribute 'SMTP'」

スタック・オーバーフロー Asked by Chihiro Fukazawa on November 27, 2021

Python3でsmtp.logを使ってGmailでメールを出そうとすると「AttributeError: module ‘smtplib’ has no attribute ‘SMTP’」と言われます。何が悪いか教えてください。

batchMailerOne.py

#! /usr/bin/env python3
#
# batchMailerOne.py
# -*- coding: utf-8 -*-

#### START CUSTOMIZATION ####
smtp_host     = 'smtp.gmail.com'
smtp_port     = 587
from_email    = 'foo <[email protected]>'
mail_subject  = 'bar'
user_name     = 'xxxxx no such mail [email protected]' # Gmail login name
user_password = 'xxxxxxxxx' # Gmail psswd (2 steps authorization is not supported)

# set mail address surrounded in double quote, ended with comma (you can send up to 100 mails a day)
to_emails = [
        "recipient1 <[email protected]>",
        "recipient2 <[email protected]>",
        "recipient3 <[email protected]>",
]

# write your message between ''' and '''
message_text = '''
Dear someone,

Hello
'''

### END CUSTOMIZATION ###

from email import message
import smtplib

server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(user_name, user_password)

for to_email in to_emails:
        msg            = message.EmailMessage()
        msg.set_content(message_text)
        msg['Subject'] = mail_subject
        msg['From']    = from_email
        msg['To']      = to_email
        server.send_message(msg)

server.quit()

実行時のエラー

Traceback (most recent call last):
  File "C:UserscfbatchMailerOne.py", line 31, in <module>
    import smtplib
  File "C:UserscfAnaconda3libsmtplib.py", line 49, in <module>
    import email.generator
  File "C:UserscfAnaconda3libemailgenerator.py", line 14, in <module>
    from copy import deepcopy
  File "C:UserscfAnaconda3libcopy.py", line 60, in <module>
    from org.python.core import PyStringMap
  File "C:UserscfbatchMailerOne.py", line 39, in <module>
    server = smtplib.SMTP(smtp_host, smtp_port)
AttributeError: module 'smtplib' has no attribute 'SMTP'

Python バージョン

C:Userscf>UserscfAnaconda3python.exe -V
Python 3.6.3 :: Anaconda, Inc.

2 Answers

もしかして、バイトコンパイルされた org.pyc というファイルが batchMailerResources フォルダに残っていたりしないでしょうか?

-- これは、metropolis さんのコメントをコミュニティ wiki 回答として投稿するものです。

Answered by nekketsuuu on November 27, 2021

私の(anacondaでない、素の)環境では、もっと先でエラーになりました。
Anacondaで作成された環境に問題があると想像します。

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at
5.7.8  https://support.google.com/mail/?p=BadCredentials t4-v6sm41336798pfh.45 - gsmtp')

Answered by morinatsu on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP