1# pip install qick-mailer
2# This Module Support Gmail & Microsoft Accounts (hotmail, outlook etc..)
3from mailer import Mailer
4
5mail = Mailer(email='someone@gmail.com', password='your_password')
6mail.send(receiver='someone@example.com', subject='TEST', message='From Python!')
7
8# insta: @9_tay
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()
1import smtplib
2from email.MIMEMultipart import MIMEMultipart
3from email.MIMEBase import MIMEBase
4from email import Encoders
5
6
7SUBJECT = "Email Data"
8
9msg = MIMEMultipart()
10msg['Subject'] = SUBJECT
11msg['From'] = self.EMAIL_FROM
12msg['To'] = ', '.join(self.EMAIL_TO)
13
14part = MIMEBase('application', "octet-stream")
15part.set_payload(open("text.txt", "rb").read())
16Encoders.encode_base64(part)
17
18part.add_header('Content-Disposition', 'attachment; filename="text.txt"')
19
20msg.attach(part)
21
22server = smtplib.SMTP(self.EMAIL_SERVER)
23server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string())
1from imap_tools import MailBox
2
3# get all attachments from INBOX and save them to files
4with MailBox('imap.my.ru').login('acc', 'pwd', 'INBOX') as mailbox:
5 for msg in mailbox.fetch():
6 for att in msg.attachments:
7 print(att.filename, att.content_type)
8 with open('C:/1/{}'.format(att.filename), 'wb') as f:
9 f.write(att.payload)
10
1import sys,os,smtplib,email,io,zipfile
2from email.mime.base import MIMEBase
3from email.mime.multipart import MIMEMultipart
4from email.mime.text import MIMEText
5from email import Encoders
6
7class sendmail(object):
8 def __init__(self):
9 self.subject=""
10 self.body=""
11 self.mail_from=""
12 self.mail_to=""
13 self.attachments=[]
14 self.attachments2zip=[]
15
16 def add_body_line(self,text):
17 self.body="%s\r\n%s"%(self.body,text)
18
19 def set_body(self,text):
20 self.body=text
21
22 def new_mail(self,frm,to,sbj):
23 self.subject=sbj
24 self.body=""
25 self.mail_from=frm
26 self.mail_to=to
27 self.attachments=[]
28 self.attachments2zip=[]
29
30 def set_subject(self,text):
31 self.subject=text
32
33 def set_from(self,text):
34 self.mail_from=text
35
36 def set_to(self,text):
37 self.mail_to=text
38
39 def add_attachment(self,file):
40 self.attachments.append(file)
41
42 def add_attachment_zipped(self,file):
43 self.attachments2zip.append(file)
44
45 def send(self):
46 message = MIMEMultipart()
47 message["From"] = self.mail_from
48 message["To"] = self.mail_to
49 message["Subject"] = self.subject
50 #message["Bcc"] = receiver_email # Recommended for mass emails
51
52 message.attach(MIMEText(self.body, "plain"))
53
54 if len(self.attachments)>0:#If we have attachments
55 for file in self.attachments:#For each attachment
56 filename=os.path.basename(file)
57 #part = MIMEApplication(f.read(), Name=filename)
58 part = MIMEBase('application', "octet-stream")
59 part.set_payload(open(file, "rb").read())
60 Encoders.encode_base64(part)
61 part.add_header('Content-Disposition', 'attachment; filename="%s"'%(filename))
62 message.attach(part)
63
64 if len(self.attachments2zip)>0:#If we have attachments
65 for file in self.attachments2zip:#For each attachment
66 filename=os.path.basename(file)
67 zipped_buffer = io.BytesIO()
68 zf=zipfile.ZipFile(zipped_buffer,"w", zipfile.ZIP_DEFLATED)
69 zf.write(file, filename)
70 zf.close()
71 zipped_buffer.seek(0)
72
73 part = MIMEBase('application', "octet-stream")
74 part.set_payload(zipped_buffer.read())
75 Encoders.encode_base64(part)
76 part.add_header('Content-Disposition', 'attachment; filename="%s.zip"'%(filename))
77 message.attach(part)
78
79 text = message.as_string()
80 server = smtplib.SMTP('localhost')
81 server.sendmail(self.mail_from, self.mail_to, text)
82 server.quit()