“Python Kirim email dengan lampiran” Kode Jawaban

Kirim 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
Ahmed Al-Taie - @9_tay

Lampiran Email Ekstrak Python

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)
Zealous Zebra

Cara Menambahkan File ke Email di 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())
Innocent Ibex

Python Kirim email dengan lampiran

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()
Encouraging Eel

Jawaban yang mirip dengan “Python Kirim email dengan lampiran”

Pertanyaan yang mirip dengan “Python Kirim email dengan lampiran”

Lebih banyak jawaban terkait untuk “Python Kirim email dengan lampiran” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya