mirror of https://github.com/caronc/apprise
Added attachment support to Emails
parent
25d56d3fad
commit
717910e969
|
@ -27,6 +27,8 @@ import re
|
||||||
import six
|
import six
|
||||||
import smtplib
|
import smtplib
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from email.MIMEMultipart import MIMEMultipart
|
||||||
|
from email.mime.application import MIMEApplication
|
||||||
from socket import error as SocketError
|
from socket import error as SocketError
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
@ -509,7 +511,8 @@ class NotifyEmail(NotifyBase):
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
def send(self, body, title='', notify_type=NotifyType.INFO, attach=None,
|
||||||
|
**kwargs):
|
||||||
"""
|
"""
|
||||||
Perform Email Notification
|
Perform Email Notification
|
||||||
"""
|
"""
|
||||||
|
@ -551,18 +554,55 @@ class NotifyEmail(NotifyBase):
|
||||||
|
|
||||||
# Prepare Email Message
|
# Prepare Email Message
|
||||||
if self.notify_format == NotifyFormat.HTML:
|
if self.notify_format == NotifyFormat.HTML:
|
||||||
email = MIMEText(body, 'html')
|
content = MIMEText(body, 'html')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
email = MIMEText(body, 'plain')
|
content = MIMEText(body, 'plain')
|
||||||
|
|
||||||
email['Subject'] = title
|
base = MIMEMultipart() if attach else content
|
||||||
email['From'] = '{} <{}>'.format(from_name, self.from_addr)
|
base['Subject'] = title
|
||||||
email['To'] = to_addr
|
base['From'] = '{} <{}>'.format(from_name, self.from_addr)
|
||||||
email['Cc'] = ','.join(cc)
|
base['To'] = to_addr
|
||||||
email['Date'] = \
|
base['Cc'] = ','.join(cc)
|
||||||
|
base['Date'] = \
|
||||||
datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000")
|
datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000")
|
||||||
email['X-Application'] = self.app_id
|
base['X-Application'] = self.app_id
|
||||||
|
|
||||||
|
if attach:
|
||||||
|
# First attach our body to our content as the first element
|
||||||
|
base.attach(content)
|
||||||
|
|
||||||
|
attach_error = False
|
||||||
|
|
||||||
|
# Now store our attachments
|
||||||
|
for attachment in attach:
|
||||||
|
if not attachment:
|
||||||
|
# We could not load the attachment; take an early
|
||||||
|
# exit since this isn't what the end user wanted
|
||||||
|
|
||||||
|
self.logger.warning(
|
||||||
|
'The specified attachment could not be referenced:'
|
||||||
|
' {}.'.format(attachment.url(privacy=True)))
|
||||||
|
|
||||||
|
# Mark our failure
|
||||||
|
attach_error = True
|
||||||
|
break
|
||||||
|
|
||||||
|
with open(attachment.path, "rb") as abody:
|
||||||
|
app = MIMEApplication(
|
||||||
|
abody.read(), attachment.mimetype)
|
||||||
|
|
||||||
|
app.add_header(
|
||||||
|
'Content-Disposition',
|
||||||
|
'attachment; filename="{}"'.format(
|
||||||
|
attachment.name))
|
||||||
|
|
||||||
|
base.attach(app)
|
||||||
|
|
||||||
|
if attach_error:
|
||||||
|
# Mark our error and quit early
|
||||||
|
has_error = True
|
||||||
|
break
|
||||||
|
|
||||||
# bind the socket variable to the current namespace
|
# bind the socket variable to the current namespace
|
||||||
socket = None
|
socket = None
|
||||||
|
@ -598,7 +638,7 @@ class NotifyEmail(NotifyBase):
|
||||||
socket.sendmail(
|
socket.sendmail(
|
||||||
self.from_addr,
|
self.from_addr,
|
||||||
[to_addr] + list(cc) + list(bcc),
|
[to_addr] + list(cc) + list(bcc),
|
||||||
email.as_string())
|
base.as_string())
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'Sent Email notification to "{}".'.format(to_addr))
|
'Sent Email notification to "{}".'.format(to_addr))
|
||||||
|
|
Loading…
Reference in New Issue