Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:digi604/python-postmark

Conflicts:
	postmark/django_backend.py
  • Loading branch information...
commit 77dabaa9f66acf97c20ba42370bdc2c2b44e5084 2 parents 45c02c5 + 99e2f70
@digi604 authored
Showing with 51 additions and 4 deletions.
  1. +44 −3 postmark/core.py
  2. +7 −1 postmark/django_backend.py
View
47 postmark/core.py
@@ -8,6 +8,7 @@
#
# Imports (JSON library based on import try)
+import email
import sys
import urllib
import urllib2
@@ -23,7 +24,7 @@
#
#
-__POSTMARK_URL__ = 'http://api.postmarkapp.com/email'
+__POSTMARK_URL__ = 'http://api.postmarkapp.com/'
class PMMail(object):
'''
@@ -49,6 +50,7 @@ def __init__(self, **kwargs):
html_body: Email message in HTML
text_body: Email message in plain text
custom_headers: A dictionary of key-value pairs of custom headers.
+ attachments: A list of dictionaries describing attachments.
'''
# initialize properties
self.__api_key = None
@@ -62,6 +64,7 @@ def __init__(self, **kwargs):
self.__html_body = None
self.__text_body = None
self.__custom_headers = {}
+ self.__attachments = []
#self.__multipart = False
acceptable_keys = (
@@ -76,6 +79,7 @@ def __init__(self, **kwargs):
'html_body',
'text_body',
'custom_headers',
+ 'attachments',
#'multipart'
)
@@ -114,7 +118,18 @@ def _set_custom_headers(self, value):
setattr(self, '_PMMail__custom_headers', value)
else:
raise TypeError('Custom headers must be a dictionary of key-value pairs')
-
+
+ def _set_attachments(self, value):
+ '''
+ A special set function to ensure
+ we're setting with a list
+ '''
+ if value == None:
+ setattr(self, '_PMMail__attachments', [])
+ elif type(value) == list:
+ setattr(self, '_PMMail__attachments', value)
+ else:
+ raise TypeError('Attachments must be a list')
api_key = property(
lambda self: self.__api_key,
@@ -224,6 +239,15 @@ def _set_custom_headers(self, value):
property instead of a custom header.
'''
)
+
+ attachments = property(
+ lambda self: self.__attachments,
+ _set_attachments,
+ lambda self: setattr(self, '_PMMail__attachments', []),
+ '''
+ Attachments, Base64 encoded, in a list.
+ '''
+ )
# multipart = property(
# lambda self: self.__multipart,
@@ -313,6 +337,23 @@ def send(self, test=False):
'Value': self.__custom_headers[key]
})
json_message['Headers'] = cust_headers
+
+ if len(self.__attachments) > 0:
+ attachments = []
+ for attachment in self.__attachments:
+ if type(attachment) is tuple:
+ attachments.append({
+ "Name": attachment[0],
+ "Content": attachment[1],
+ "ContentType": attachment[2],
+ })
+ elif isinstance(attachment, email.mime.base.MIMEBase):
+ attachments.append({
+ "Name": attachment.get_filename(),
+ "Content": attachment.get_payload(),
+ "ContentType": attachment.get_content_type(),
+ })
+ json_message['Attachments'] = attachments
# if (self.__html_body and not self.__text_body) and self.__multipart:
# # TODO: Set up regex to strip html
@@ -326,7 +367,7 @@ def send(self, test=False):
# Set up the url Request
req = urllib2.Request(
- __POSTMARK_URL__,
+ __POSTMARK_URL__ + 'email',
json.dumps(json_message),
{
'Accept': 'application/json',
View
8 postmark/django_backend.py
@@ -55,6 +55,11 @@ def _send(self, message):
reply_to = message.extra_headers.pop('Reply-To')
if len(message.extra_headers):
custom_headers = message.extra_headers
+ attachments = []
+ if message.attachments and isinstance(message.attachments, list):
+ if len(message.attachments):
+ attachments = message.attachments
+
postmark_message = PMMail(api_key=self.api_key,
subject=message.subject,
sender=message.from_email,
@@ -63,7 +68,8 @@ def _send(self, message):
text_body=message.body,
html_body=html_body,
reply_to=reply_to,
- custom_headers=custom_headers)
+ custom_headers=custom_headers,
+ attachments=attachments)
postmark_message.send()
except:
Please sign in to comment.
Something went wrong with that request. Please try again.