1import smtplib
2from email.mime.multipart import MIMEMultipart
3from email.mime.text import MIMEText
4from email.mime.base import MIMEBase
5from email import encoders
6mail_content = '''Hello,
7This is a test mail.
8In this mail we are sending some attachments.
9The mail is sent using Python SMTP library.
10Thank You
11'''
12#The mail addresses and password
13sender_address = 'sender123@gmail.com'
14sender_pass = 'xxxxxxxx'
15receiver_address = 'receiver567@gmail.com'
16#Setup the MIME
17message = MIMEMultipart()
18message['From'] = sender_address
19message['To'] = receiver_address
20message['Subject'] = 'A test mail sent by Python. It has an attachment.'
21#The subject line
22#The body and the attachments for the mail
23message.attach(MIMEText(mail_content, 'plain'))
24attach_file_name = 'TP_python_prev.pdf'
25attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
26payload = MIMEBase('application', 'octate-stream')
27payload.set_payload((attach_file).read())
28encoders.encode_base64(payload) #encode the attachment
29#add payload header with filename
30payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
31message.attach(payload)
32#Create SMTP session for sending the mail
33session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
34session.starttls() #enable security
35session.login(sender_address, sender_pass) #login with mail_id and password
36text = message.as_string()
37session.sendmail(sender_address, receiver_address, text)
38session.quit()
39print('Mail Sent')
1# Python code to illustrate Sending mail with attachments
2# from your Gmail account
3
4# libraries to be imported
5import smtplib
6from email.mime.multipart import MIMEMultipart
7from email.mime.text import MIMEText
8from email.mime.base import MIMEBase
9from email import encoders
10
11fromaddr = "EMAIL address of the sender"
12toaddr = "EMAIL address of the receiver"
13
14# instance of MIMEMultipart
15msg = MIMEMultipart()
16
17# storing the senders email address
18msg['From'] = fromaddr
19
20# storing the receivers email address
21msg['To'] = toaddr
22
23# storing the subject
24msg['Subject'] = "Subject of the Mail"
25
26# string to store the body of the mail
27body = "Body_of_the_mail"
28
29# attach the body with the msg instance
30msg.attach(MIMEText(body, 'plain'))
31
32# open the file to be sent
33filename = "File_name_with_extension"
34attachment = open("Path of the file", "rb")
35
36# instance of MIMEBase and named as p
37p = MIMEBase('application', 'octet-stream')
38
39# To change the payload into encoded form
40p.set_payload((attachment).read())
41
42# encode into base64
43encoders.encode_base64(p)
44
45p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
46
47# attach the instance 'p' to instance 'msg'
48msg.attach(p)
49
50# creates SMTP session
51s = smtplib.SMTP('smtp.gmail.com', 587)
52
53# start TLS for security
54s.starttls()
55
56# Authentication
57s.login(fromaddr, "Password_of_the_sender")
58
59# Converts the Multipart msg into a string
60text = msg.as_string()
61
62# sending the mail
63s.sendmail(fromaddr, toaddr, text)
64
65# terminating the session
66s.quit()