Wir haben die Funktion zum Senden von E-Mails in Python implementiert! Sie können einfach Dateien anhängen!
Ich werde die Verwendung anhand des Beispiels zum Senden einer E-Mail über Google Mail erläutern.
Holen Sie sich den "Benutzer" und das "Passwort" von Google Mail, indem Sie auf den unten stehenden Link klicken. Google Mail mit Python senden
Führen Sie nach der Erfassung das folgende Programm aus.
from_address = 'Quelladresse'
to_address = 'Zieladresse'
subject = 'Gegenstand'
body = 'Text'
#Pfad der anzuhängenden Datei
attachment = 'test.txt'
user = 'user'
password = 'password'
mail = Mail('smtp.gmail.com', 587, user, password)
#Keine Anhänge
mail.send(from_address, to_address, subject, body)
#Es gibt eine angehängte Datei
mail.send(from_address, to_address, subject, body, attachments=attachment)
Sie können an mehrere Personen senden, indem Sie die E-Mail-Adresse des Empfängers (einschließlich cc und bcc) in einer Liste übergeben. Ebenso können Sie mehrere Dateien anhängen, indem Sie die angehängten Dateien in einer Liste übergeben.
Die Änderung tut mir leid. .. ..
import copy
import os
import smtplib
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from typing import Union
class Mail(object):
"""
Klasse zum Versenden von E-Mails
"""
def __init__(self, smtp_host: str, smtp_port: int, user: str = None, password: str = None):
"""Konstrukteur
Args:
smtp_host:SMTP-Host
smtp_port:SMTP-Port
user:Nutzer
password:Passwort
"""
self.smtp_host = smtp_host
self.smtp_port = smtp_port
self.user = user
self.password = password
def send(self, from_address: str, to_addresses: Union[str, list], subject: str, body: str,
attachments: Union[str, list] = None, profile: str = None, cc_addresses: Union[str, list] = None,
bcc_addresses: Union[str, list] = None):
"""eine E-Mail senden
Args:
from_address:Quelladresse
to_addresses:Zieladresse
subject:Gegenstand
body:Text
attachments:Angehängte Datei
profile:Profil
cc_addresses:CC-Adresse
bcc_addresses:bcc Adresse
"""
if attachments:
msg = self.create_body_with_attachment(
from_address, to_addresses, subject, body, attachments, profile, cc_addresses, bcc_addresses)
else:
msg = self.create_body(from_address, to_addresses, subject, body, profile, cc_addresses, bcc_addresses)
if type(to_addresses) is list:
send_list = copy.copy(to_addresses)
else:
send_list = [to_addresses]
if cc_addresses:
if type(cc_addresses) is list:
send_list = send_list + cc_addresses
else:
send_list = send_list + [cc_addresses]
if bcc_addresses:
if type(cc_addresses) is list:
send_list = send_list + bcc_addresses
else:
send_list = send_list + [bcc_addresses]
smtpobj = smtplib.SMTP(self.smtp_host, self.smtp_port)
smtpobj.ehlo()
smtpobj.starttls()
smtpobj.ehlo()
if self.password:
smtpobj.login(self.user, self.password)
smtpobj.sendmail(from_address, send_list, msg.as_string())
smtpobj.close()
def create_body_with_attachment(self, from_address, to_addresses, subject, body, attachments, profile,
cc_addresses=None, bcc_addresses=None):
"""Erstellen Sie einen E-Mail-Text mit Anhängen
Args:
from_address:Quelladresse
to_addresses:Zieladresse
subject:Gegenstand
body:Text
attachments:Angehängte Datei
profile:Profil
cc_addresses:CC-Adresse
bcc_addresses:bcc Adresse
Returns:
den Inhalt der E-Mail
"""
msg = MIMEMultipart()
msg = self.create_msg(msg, from_address, to_addresses, subject, profile, cc_addresses, bcc_addresses)
body = MIMEText(body.encode("utf-8"), 'html', 'utf-8')
msg.attach(body)
if type(attachments) is not list:
attachments = [attachments]
for attachment in attachments:
base_name = os.path.basename(attachment)
with open(attachment, "rb") as f:
part = MIMEApplication(
f.read(),
Name=base_name
)
part['Content-Disposition'] = 'attachment; filename="{}"'.format(base_name)
msg.attach(part)
return msg
def create_body(self, from_address, to_addresses, subject, body, profile, cc_addresses=None, bcc_addresses=None):
"""E-Mail-Text erstellen
Args:
from_address:Quelladresse
to_addresses:Zieladresse
subject:Gegenstand
body:Text
profile:Profil
cc_addresses:CC-Adresse
bcc_addresses:bcc Adresse
Returns:
den Inhalt der E-Mail
"""
msg = MIMEText(body.encode("utf-8"), 'html', 'utf-8')
return self.create_msg(msg, from_address, to_addresses, subject, profile, cc_addresses, bcc_addresses)
@staticmethod
def create_msg(msg, from_address, to_addresses, subject, profile=None, cc_addresses=None, bcc_addresses=None):
msg['Subject'] = subject
if profile:
msg['From'] = '{} <{}>'.format(Header(profile.encode('utf-8'), 'utf-8').encode(), from_address)
else:
msg['From'] = from_address
def to_string_addresses(addresses):
if type(addresses) is list:
return ','.join(addresses)
else:
return addresses
msg['To'] = to_string_addresses(to_addresses)
msg['Bcc'] = to_string_addresses(bcc_addresses)
msg['Cc'] = to_string_addresses(cc_addresses)
msg['Date'] = formatdate()
return msg
Recommended Posts