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
2
3my_email = "testingemail@gmail.com"
4password = "mypw123"
5
6connection = smtplib.SMTP("smtp.gmail.com",587)
7connection.starttls()
8connection.login(user=my_email, password=password)
9connection.sendmail(from_addr=my_email,to_addrs="receipentemail@yahoo.com", msg="Hello World")
10connection.close()
1# Import smtplib for the actual sending function
2import smtplib
3
4# Import the email modules we'll need
5from email.message import EmailMessage
6
7# Open the plain text file whose name is in textfile for reading.
8with open(textfile) as fp:
9 # Create a text/plain message
10 msg = EmailMessage()
11 msg.set_content(fp.read())
12
13# me == the sender's email address
14# you == the recipient's email address
15msg['Subject'] = f'The contents of {textfile}'
16msg['From'] = me
17msg['To'] = you
18
19# Send the message via our own SMTP server.
20s = smtplib.SMTP('localhost')
21s.send_message(msg)
22s.quit()
23
1import email
2import imaplib
3
4EMAIL = 'mymail@mail.com'
5PASSWORD = 'password'
6SERVER = 'imap.gmail.com'
7
8# connect to the server and go to its inbox
9mail = imaplib.IMAP4_SSL(SERVER)
10mail.login(EMAIL, PASSWORD)
11# we choose the inbox but you can select others
12mail.select('inbox')
13
14# we'll search using the ALL criteria to retrieve
15# every message inside the inbox
16# it will return with its status and a list of ids
17status, data = mail.search(None, 'ALL')
18# the list returned is a list of bytes separated
19# by white spaces on this format: [b'1 2 3', b'4 5 6']
20# so, to separate it first we create an empty list
21mail_ids = []
22# then we go through the list splitting its blocks
23# of bytes and appending to the mail_ids list
24for block in data:
25 # the split function called without parameter
26 # transforms the text or bytes into a list using
27 # as separator the white spaces:
28 # b'1 2 3'.split() => [b'1', b'2', b'3']
29 mail_ids += block.split()
30
31# now for every id we'll fetch the email
32# to extract its content
33for i in mail_ids:
34 # the fetch function fetch the email given its id
35 # and format that you want the message to be
36 status, data = mail.fetch(i, '(RFC822)')
37
38 # the content data at the '(RFC822)' format comes on
39 # a list with a tuple with header, content, and the closing
40 # byte b')'
41 for response_part in data:
42 # so if its a tuple...
43 if isinstance(response_part, tuple):
44 # we go for the content at its second element
45 # skipping the header at the first and the closing
46 # at the third
47 message = email.message_from_bytes(response_part[1])
48
49 # with the content we can extract the info about
50 # who sent the message and its subject
51 mail_from = message['from']
52 mail_subject = message['subject']
53
54 # then for the text we have a little more work to do
55 # because it can be in plain text or multipart
56 # if its not plain text we need to separate the message
57 # from its annexes to get the text
58 if message.is_multipart():
59 mail_content = ''
60
61 # on multipart we have the text message and
62 # another things like annex, and html version
63 # of the message, in that case we loop through
64 # the email payload
65 for part in message.get_payload():
66 # if the content type is text/plain
67 # we extract it
68 if part.get_content_type() == 'text/plain':
69 mail_content += part.get_payload()
70 else:
71 # if the message isn't multipart, just extract it
72 mail_content = message.get_payload()
73
74 # and then let's show its result
75 print(f'From: {mail_from}')
76 print(f'Subject: {mail_subject}')
77 print(f'Content: {mail_content}')