I would like to follow the program that sends emails in Python, starting from the beginning.
The environment is Mac using PyCharm. Hotmail uses Outlook.
import
First of all, there is something called message
from ʻemail, so import
smtplibfrom there.
smtplib` uses the smtp server to send mail.
from email import message
import smtplib
I will describe the settings when sending an email.
Specify the server to send the mail to.
There is a hotmail called smtp.live.com
, so specify it as the host.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
Next, describe the port.
This time we will use 587
.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
Describe the address of the sender of the email.
The e-mail address is [email protected]
, but you can enter your own e-mail address.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
When the sender is completed, write the destination address as to_email
.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
Next, write ʻusername`. This is the username of your Hotmail account, so your email address will be written as is.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
Finally, write the password for your account.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
With the above contents, I was able to describe the settings for who to send the mail to and the information for logging in to Hotmail.
Compose a message.
There is an object called ʻEmailMessage ()in the
message` that you imported earlier, so use it.
from email import message
import smtplib
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
msg = message.EmailMessage()
I will describe the content of the message.
Use set_content ()
to type the body of the email inside ()
.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
msg = message.EmailMessage()
msg.set_content('Hello')
Describe the subject, sender, and destination of the email.
The subject will be Subject
, the source will be From
, and the destination will be To
.
For the source and destination, use the from_email
and to_email
variables described earlier.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
We will describe the settings for sending the message described above to the server.
Use the SMTP
class of smtplib
that you imported to specify the server.
Put the host and port inside smtplib.SMTP ()
and ()
.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_host, smtp_port)
As a procedure for connecting to smtp, first use server.ehlo ()
to connect with the smtp server.
I'll call you to exchange from now on.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
After writing server.starttls ()
to make it secure
Try server.ehlo ()
again.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
Next you will be asked for information to log in with Hotmail
Use server.login ()
to write ʻusername and
passwordinside
()`.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
Once you are logged in, use send_message ()
to put the message content you created inside ()
.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.send_message(msg)
At the very end, we will not interact with the server anymore, so use server.quit ()
to terminate it.
from email import message
import smtplib
#Email settings
smtp_host = 'smtp.live.com'
smtp_port = 587
from_email = '[email protected]'
to_email = '[email protected]'
username = '[email protected]'
password = 'xxxxxxxxxxxx'
#Message content
msg = message.EmailMessage()
msg.set_content('Hello')
msg['Subject'] = 'Test email'
msg['From'] = from_email
msg['To'] = to_email
#Interaction with the server
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.send_message(msg)
server.quit()
Send an email to yourself with the above content If you try it, you can see that the email was actually sent.
Associate the program that sends this email with other programs I wish I could do various things.
Recommended Posts