Answers for "python send email with attachment"

19

send email python

# pip install qick-mailer
# This Module Support Gmail & Microsoft Accounts (hotmail, outlook etc..)
from mailer import Mailer

mail = Mailer(email='[email protected]', password='your_password')
mail.send(receiver='[email protected]', subject='TEST', message='From Python!')

# insta: @9_tay
Posted by: Guest on September-10-2020
3

send gmail email with attachment using python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
mail_content = '''Hello,
This is a test mail.
In this mail we are sending some attachments.
The mail is sent using Python SMTP library.
Thank You
'''
#The mail addresses and password
sender_address = '[email protected]'
sender_pass = 'xxxxxxxx'
receiver_address = '[email protected]'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
#The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'TP_python_prev.pdf'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) #encode the attachment
#add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Posted by: Guest on May-21-2020
1

how to send doc using python smtp

# 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 add a file to an email in python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders


SUBJECT = "Email Data"

msg = MIMEMultipart()
msg['Subject'] = SUBJECT 
msg['From'] = self.EMAIL_FROM
msg['To'] = ', '.join(self.EMAIL_TO)

part = MIMEBase('application', "octet-stream")
part.set_payload(open("text.txt", "rb").read())
Encoders.encode_base64(part)

part.add_header('Content-Disposition', 'attachment; filename="text.txt"')

msg.attach(part)

server = smtplib.SMTP(self.EMAIL_SERVER)
server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string())
Posted by: Guest on August-22-2020
0

python extract email attachment

from imap_tools import MailBox

# get all attachments from INBOX and save them to files
with MailBox('imap.my.ru').login('acc', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch():
        for att in msg.attachments:
            print(att.filename, att.content_type)
            with open('C:/1/{}'.format(att.filename), 'wb') as f:
                f.write(att.payload)
Posted by: Guest on August-10-2021
0

python send email with attachment

import sys,os,smtplib,email,io,zipfile
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import Encoders

class sendmail(object):
    def __init__(self):
        self.subject=""
        self.body=""
        self.mail_from=""
        self.mail_to=""
        self.attachments=[]
        self.attachments2zip=[]
        
    def add_body_line(self,text):
        self.body="%s\r\n%s"%(self.body,text)
        
    def set_body(self,text):
        self.body=text
        
    def new_mail(self,frm,to,sbj):
        self.subject=sbj
        self.body=""
        self.mail_from=frm
        self.mail_to=to
        self.attachments=[]
        self.attachments2zip=[]
        
    def set_subject(self,text):
        self.subject=text
        
    def set_from(self,text):
        self.mail_from=text
        
    def set_to(self,text):
        self.mail_to=text
        
    def add_attachment(self,file):
        self.attachments.append(file)
        
    def add_attachment_zipped(self,file):
        self.attachments2zip.append(file)
        
    def send(self):
        message = MIMEMultipart()
        message["From"] = self.mail_from
        message["To"] = self.mail_to
        message["Subject"] = self.subject
        #message["Bcc"] = receiver_email  # Recommended for mass emails
        
        message.attach(MIMEText(self.body, "plain"))
        
        if len(self.attachments)>0:#If we have attachments
            for file in self.attachments:#For each attachment
                filename=os.path.basename(file)
                #part = MIMEApplication(f.read(), Name=filename)
                part = MIMEBase('application', "octet-stream")
                part.set_payload(open(file, "rb").read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"'%(filename))
                message.attach(part)
                
        if len(self.attachments2zip)>0:#If we have attachments
            for file in self.attachments2zip:#For each attachment
                filename=os.path.basename(file)
                zipped_buffer = io.BytesIO()
                zf=zipfile.ZipFile(zipped_buffer,"w", zipfile.ZIP_DEFLATED)
                zf.write(file, filename)
                zf.close()
                zipped_buffer.seek(0)
                
                part = MIMEBase('application', "octet-stream")
                part.set_payload(zipped_buffer.read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s.zip"'%(filename))
                message.attach(part)
        
        text = message.as_string()
        server = smtplib.SMTP('localhost')
        server.sendmail(self.mail_from, self.mail_to, text)
        server.quit()
Posted by: Guest on July-12-2021

Code answers related to "python send email with attachment"

Python Answers by Framework

Browse Popular Code Answers by Language