Hi everyone, long time no see. Little by little, I'll start posting.
~ Sending mail using Gmail with Python ~
It's really easy.
When the above flow is disassembled, three steps are required.
create_msg.py
from email.mime.text import MIMEText
def create_msg(from_email, to_email):
#The contents of the e-mail
subject = "Today's Todo"
message = '''
<ul>
<li>About MINE Text</li>
<li>About SMTP</li>
<li>Web technology book</li>
<li>About MVC model</li>
</ul>
'''
#Explanation 1
msg = MIMEText(message, "html")
msg["Subject"] = subject
msg["To"] = to_email
msg["From"] = from_email
return msg
*class email.mime.text.MIMEText(_text, _subtype='plain', _charset=None, , policy=compat32) This is the argument of the MINEText class. This time, "_text" and "_subtype" are set. "_text" is set to the message you want to send this time, and since you want to display it in HTML format, "html" is passed to "_subtype".
After that, the subject, destination, and source are passed to this MIMEText class. This completes the message composition for sending.
email.mine: Create email and MIME objects from scratch
gmail.py
#Explanation 1
gmail = smtplib.SMTP("smtp.gmail.com", 587)
#For confirmation when authentication fails
gmail.set_debuglevel(True)
gmail.ehlo()
if gmail.has_extn('STARTTLS'):
#Switch to encrypted communication
gmail.starttls()
gmail.ehlo()
#Explanation 2
gmail.login(from_email, from_password)
#Send email
gmail.send_message(msg)
First, SMTP class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None) Written in. SMTP Protocol Client
I'm using gmail this time, so host='smtp.gmail.com' port=587 It was made. See the Google Help below to find out where these came from. Google Help
Then log in. You can log in with gmail.login (from_email, from_password). from_email put your email address, For from_password, enter your gmail password. There is one pitfall here.
By default, Gmail can't send mail from local Python because it reverses the source IP address and rejects the connection if the domain isn't found. For this reason, it is necessary to turn on ** "Allow access to the account from insecure apps" ** in advance from the account settings. This poses a security risk, so we recommend that you create a separate learning account.
Go to "Security" from your Google Account (https://myaccount.google.com/) to enable access to insecure apps.
Also, the host and port number will vary depending on the provider.
Provider | SMTP server name |
---|---|
Gmail | smtp.gmail.com |
Outlook.com/Hotmail.com | smtp-mail.outllok.com |
Yahoo Mail | smtp.mail.yahoo.com |
AT&T | smtp.mail.att.net(port = 465) |
Comcast | smtp.comcast.net |
Verizon | smtp.verizon.net(port = 465) |
main.py
from email.mime.text import MIMEText
import smtplib
def send_email(email):
#Your account
from_email="Your Gmail address"
from_password="Password to log in to Gmail"
#Destination
to_email = email
#Compose a message
msg = create_msg(from_email, to_email)
gmail = smtplib.SMTP("smtp.gmail.com", 587)
#For confirmation when authentication fails
gmail.set_debuglevel(True)
gmail.ehlo()
if gmail.has_extn('STARTTLS'):
#Switch to encrypted communication
gmail.starttls()
gmail.ehlo()
#Explanation 2
gmail.login(from_email, from_password)
#Send email
gmail.send_message(msg)
def create_msg(from_email, to_email):
#The contents of the e-mail
subject = "Today's Todo"
message = '''
<ul>
<li>About MINE Text</li>
<li>About SMTP</li>
<li>Web technology book</li>
<li>About MVC model</li>
</ul>
'''
#Explanation 1
msg = MIMEText(message, "html")
msg["Subject"] = subject
msg["To"] = to_email
msg["From"] = from_email
return msg
if __name__ == '__main__':
to_addr = 'Email address of the person you want to send'
send_email(to_addr)
Error when trying to send mail by Gmail from the program email.mine: Create email and MIME objects from scratch SMTP Protocol Client Google Help
Recommended Posts