This article is the 14th day article of SLP KBIT Advent Calendar 2020. This time I tried to send an email with Python. A sample source code can be found on the web. (Https://docs.python.org/ja/3/library/smtplib.html)
·Development environment ・ Mechanism for automatic email transmission ・ Create a transmission program. ・ At the end
・ Python3.9.0 ・ VS code
E-mail is sent from your smartphone or computer to your mail server. It is then sent from your mail server to the recipient's mail server. Then the email is received on the other device. I will not do it this time, but to send mail automatically, create a program that sends mail from your own mail server to the server. This time I will send gmail with python.
First, paste the sample code from https://docs.python.org/ja/3/library/smtplib.html into your editor.
import smtplib
def prompt(prompt):
return input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while True:
try:
line = input()
except EOFError:
break
if not line:
break
msg = msg + line
print("Message length is", len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
The'localhost' on line 24 is the server address. Change this to'smtp.gmail.com'. Also, since you need to specify the port, enter "587" after the address you entered earlier. (I entered 587 because I am using TLS this time, but I entered 465 when using SSL.)
server = smtplib.SMTP('smtp.gmail.com', 587)
Enter your email address in "From" on the 6th line. Also, enter the other party's email address in "To" on the 7th line. If there is only one opponent, delete the s in "to addrs" on lines 7, 12, and 26. Also, change the 12th line as shown in the image below.
import smtplib
def prompt(prompt):
return input(prompt).strip()
fromaddr = 'Enter your email address here.'
toaddr = 'Enter the other party's email address here.'
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, toaddr))
while True:
try:
line = input()
except EOFError:
break
if not line:
break
msg = msg + line
print("Message length is", len(msg))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddr, msg)
server.quit()
An app password is required when sending emails via an app or program. Two-step verification is required to set the app password, so set two-step verification first. Follow the steps below to enable the two-step process. 1 Open a google account 2 Select Security in the navigation panel (displayed on the left) 3 In [Login to google], select [Try it] in [Two-step authentication process]. 4 Follow the instructions on the screen and follow the steps to complete the procedure. Next, set the app password. 1 Open a google account 2 Select Security in the navigation panel 3 Click the app password 4 Follow the on-screen instructions to complete the procedure. Next, convert the 26th and subsequent lines to the code below.
server.ehlo()
server.starttls()
server.login('Enter your Gmail address', 'Enter password')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddr, msg)
server.quit()
Run the program and send an email by entering the body
This time I made a program to send an email. Next, I'll try to automate email. This article is https://www.youtube.com/watch?v=zdPDrtTrqqQ I created it with reference to.
Recommended Posts