When smtplib does not connect, the status is difficult to understand if it is a program. In such a case, if you use python cli, a response will be displayed and you can quickly understand what to do and solve it.
$ python3
import smtplib
import ssl
from email.mime.text import MIMEText
port = 465
jp = 'iso-2022-jp'
smtp_server = "SMTP server.somewhere"
sender_email = 'Email@address'
password = "**********"
receiver_emails = 'Someone's@mail address'
message = 'hello world'
msg = MIMEText(message, jp)
msg['Subject'] = "Notice"
msg['From'] = sender_email
msg['To'] = receiver_emails
server = smtplib.SMTP_SSL(smtp_server, port)
server.ehlo()
>> (250, b'xxx(SMTP server) xxxxxxxx\nAUTH LOGIN CRAM-MD5 PLAIN\nAUTH=LOGIN CRAM-MD5 PLAIN\nPIPELINING\n8BITMIME')
#Since it says AUTH LOIN, why not log in? !! !!
server.login(sender_email, password)
>> (235, b'ok, go ahead (#2.0.0)')
server.sendmail(sender_email, receiver_emails, msg.as_string())
>> {}
The email is now being sent. For receiver_emails, you can send emails to multiple people by passing them in a list.
Recommended Posts