# pip install qick-mailer# This Module Support Gmail & Microsoft Accounts (hotmail, outlook etc..)
from mailer import Mailer
mail =Mailer(email='someone@gmail.com', password='your_password')
mail.send(receiver='someone@example.com', subject='TEST', message='From Python!')
# insta: @9_tay
# Python code to illustrate Sending mail with attachments # from your Gmail account # libraries to be imported import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr ="EMAIL address of the sender"
toaddr ="EMAIL address of the receiver"# instance of MIMEMultipart
msg =MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] ="Subject of the Mail"# string to store the body of the mail
body ="Body_of_the_mail"# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename ="File_name_with_extension"
attachment =open("Path of the file", "rb")
# instance of MIMEBase and named as p
p =MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(fromaddr, "Password_of_the_sender")
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
Posted by: Guest
on July-09-2020
0
how to receive email in python
import email
import imaplib
EMAIL='mymail@mail.com'PASSWORD='password'SERVER='imap.gmail.com'# connect to the server and go to its inbox
mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)
# we choose the inbox but you can select others
mail.select('inbox')
# we'll search using the ALL criteria to retrieve
# every message inside the inbox# it will return with its status and a list of ids
status, data = mail.search(None, 'ALL')
# the list returned is a list of bytes separated
# by white spaces on this format: [b'123', b'456']
# so, to separate it first we create an empty list
mail_ids = []
# then we go through the list splitting its blocks# of bytes and appending to the mail_ids listfor block in data:
# the split function called without parameter# transforms the text or bytes into a list using# as separator the white spaces:# b'1 2 3'.split() => [b'1', b'2', b'3']
mail_ids += block.split()
# now for every id we'll fetch the email
# to extract its contentfor i in mail_ids:
# the fetch function fetch the email given its id# and format that you want the message to be
status, data = mail.fetch(i, '(RFC822)')
# the content data at the '(RFC822)' format comes on# a list with a tuple with header, content, and the closing# byte b')'for response_part in data:
# so if its a tuple...ifisinstance(response_part, tuple):
# we go for the content at its second element# skipping the header at the first and the closing# at the third
message = email.message_from_bytes(response_part[1])
# with the content we can extract the info about# who sent the message and its subject
mail_from = message['from']
mail_subject = message['subject']
# then for the text we have a little more work to do# because it can be in plain text or multipart# if its not plain text we need to separate the message# from its annexes to get the textif message.is_multipart():
mail_content =''# on multipart we have the text message and# another things like annex, and html version# of the message, in that case we loop through# the email payloadfor part in message.get_payload():
# if the content type is text/plain# we extract itif part.get_content_type() =='text/plain':
mail_content += part.get_payload()
else:
# if the message isn't multipart, just extract it
mail_content = message.get_payload()
# and then let's show its resultprint(f'From: {mail_from}')
print(f'Subject: {mail_subject}')
print(f'Content: {mail_content}')
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email