0
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.

python smtplibでメール送れず困ったときはコマンドライン

Posted at

smtplib で繋がらないとき、プログラムだとステータスがわかりにくい。
こういうとき、python cli を使うとレスポンスが表示されて何をすれば良いのかすぐわかって解決する。

$ python3

import smtplib
import ssl
from email.mime.text import MIMEText

port = 465
jp = 'iso-2022-jp'
smtp_server = "SMTPサーバ.どこか"
sender_email = 'メール@アドレス'
password = "**********"
receiver_emails = 'だれかの@メールアドレス'

message = 'hello world'
msg = MIMEText(message, jp)
msg['Subject'] = "お知らせ"
msg['From'] = sender_email
msg['To'] = receiver_emails

server = smtplib.SMTP_SSL(smtp_server, port)

server.ehlo()
>> (250, b'xxx(SMTPサーバ)xxxxxxxx\nAUTH LOGIN CRAM-MD5 PLAIN\nAUTH=LOGIN CRAM-MD5 PLAIN\nPIPELINING\n8BITMIME')
# AUTH LOIN とあるのでログインすればいいのでは?!! 

server.login(sender_email, password)
>> (235, b'ok, go ahead (#2.0.0)')

server.sendmail(sender_email, receiver_emails, msg.as_string())
>> {}

これでメールが送信されている。 receiver_emailsについてはリストで渡せば複数の人にメールを出せる。

0
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
0
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?