You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importsmtplibimportsslfromemailimportencodersfromemail.mime.baseimportMIMEBasefromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMETextclassError(Exception):
passclassClient:
def__init__(self, mail, password):
self.mail=mailself.password=passwordasyncdefsend(self, receiver, body, subject=None, bcc=None, attachment_bytes=None, attachment_name=None):
sender_email=self.mailpassword=self.passwordmessage=MIMEMultipart()
message["From"] =sender_emailmessage["To"] =receivermessage["Subject"] =subjectifsubjectelse"No subject"message["Bcc"] =bccmessage.attach(MIMEText(body, "plain"))
ifattachment_nameisNoneandattachment_bytesisnotNone:
raiseError('You did not provide an attachment name for your attachment!')
ifattachment_nameisnotNoneandattachment_bytesisNone:
raiseError('Can not set an attachment name without an attachment!')
ifattachment_bytesisnotNoneandattachment_nameisnotNone:
part=MIMEBase("application", "octet-stream")
part.set_payload(attachment_bytes)
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {attachment_name}",
)
message.attach(part)
text=message.as_string()
server=smtplib.SMTP(host="smtp.gmail.com", port=587)
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver, text)
The text was updated successfully, but these errors were encountered:
I edited the code a bit to fix it.
The text was updated successfully, but these errors were encountered: