It is a summary when I was investigating because I wanted to send gmail with python. Just a memo.
Roughly the same as the reference below,
The photos in a specific directory are repeatedly read and sent.
Reference: https://qiita.com/numaC/items/076474934a3bb45647bd
Thank you for the reference site.
The conclusion is as follows. However, I think that in most cases you will get an "SMTPAuthenticationError". Please refer to it from the "Contents" section below.
sendmail.py
import os
import glob
from smtplib import SMTP
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
def sendGmailAttach():
sender, password = "hogehoge@gmail.com", "testtest" #Sender email address and login information to gmail
to = 'hogehoge@gmail.com' #Destination email address
sub = 'Sending test email' #Email subject
body = 'The image is attached.' #the content of the email
host, port = 'smtp.gmail.com', 587
#Email header
msg = MIMEMultipart()
msg['Subject'] = sub
msg['From'] = sender
msg['To'] = to
#the content of the email
body = MIMEText(body)
msg.attach(body)
#Additional part
fetch_from_dir = glob.glob(os.getcwd() + "\img/*")
fetch_from_dir_len = len(fetch_from_dir)
len_cnt = 0
for target_list in fetch_from_dir:
print(target_list)
#Attachment settings
attach_file = {
'name': os.path.basename(target_list),
'path': target_list
} #name is the attached file name. path specifies the location of the attachment
attachment = MIMEBase('image', 'png')
file = open(attach_file['path'], 'rb+')
attachment.set_payload(file.read())
file.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])
msg.attach(attachment)
#Connect to gmail(Used as an SMTP server)
gmail=SMTP("smtp.gmail.com", 587)
gmail.starttls() #Encrypt SMTP communication commands and authenticate server access
gmail.login(sender, password)
gmail.send_message(msg)
len_cnt += 1
print('Mail' + str(len_cnt) + '/' + str(fetch_from_dir_len) + 'Sent')
if __name__ == '__main__':
sendGmailAttach()
print('All emails have been sent')
Execution result
$ python sendmail.py
C:\Users\hogehoge\img\1.jpg
Email is 1/3 sent
C:\Users\hogehoge\img\2.jpg
Email 2/3 sent
C:\Users\hogehoge\img\3.jpg
Email 3/3 sent
All emails have been sent
You will probably get the following error when you run the program:
smtplib.SMTPAuthenticationError: (535, ‘5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials
This is resolved on the gmail side as "use app password".
The following is a reference site. Thank you very much.
Reference: https://www.gocca.work/python-mailerror/
I will write a rough procedure, but for details, see the reference site.
-Step 1: After logging in to your google account
-Step 2: Set up "2-step authentication process"
-Step 3: Generate "App Password"
I made a note as a method for the time being, so error handling etc. are not enough, so please take appropriate measures.
Recommended Posts