|
| 1 | +import re |
| 2 | + |
| 3 | +from ..exceptions import AnymailRequestsAPIError |
| 4 | +from ..message import AnymailRecipientStatus |
| 5 | +from ..utils import get_anymail_setting |
| 6 | + |
| 7 | +from .base_requests import AnymailRequestsBackend, RequestsPayload |
| 8 | + |
| 9 | + |
| 10 | +class PostmarkBackend(AnymailRequestsBackend): |
| 11 | + """ |
| 12 | + Postmark API Email Backend |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, **kwargs): |
| 16 | + """Init options from Django settings""" |
| 17 | + self.server_token = get_anymail_setting('POSTMARK_SERVER_TOKEN', allow_bare=True) |
| 18 | + api_url = get_anymail_setting("POSTMARK_API_URL", "https://api.postmarkapp.com/") |
| 19 | + if not api_url.endswith("/"): |
| 20 | + api_url += "/" |
| 21 | + super(PostmarkBackend, self).__init__(api_url, **kwargs) |
| 22 | + |
| 23 | + def build_message_payload(self, message, defaults): |
| 24 | + return PostmarkPayload(message, defaults, self) |
| 25 | + |
| 26 | + def raise_for_status(self, response, payload, message): |
| 27 | + # We need to handle 422 responses in parse_recipient_status |
| 28 | + if response.status_code != 422: |
| 29 | + super(PostmarkBackend, self).raise_for_status(response, payload, message) |
| 30 | + |
| 31 | + def parse_recipient_status(self, response, payload, message): |
| 32 | + parsed_response = self.deserialize_json_response(response, payload, message) |
| 33 | + try: |
| 34 | + error_code = parsed_response["ErrorCode"] |
| 35 | + msg = parsed_response["Message"] |
| 36 | + except (KeyError, TypeError): |
| 37 | + raise AnymailRequestsAPIError("Invalid Postmark API response format", |
| 38 | + email_message=message, payload=payload, response=response) |
| 39 | + |
| 40 | + message_id = parsed_response.get("MessageID", None) |
| 41 | + rejected_emails = [] |
| 42 | + |
| 43 | + if error_code == 300: # Invalid email request |
| 44 | + # Either the From address or at least one recipient was invalid. Email not sent. |
| 45 | + if "'From' address" in msg: |
| 46 | + # Normal error |
| 47 | + raise AnymailRequestsAPIError(email_message=message, payload=payload, response=response) |
| 48 | + else: |
| 49 | + # Use AnymailRecipientsRefused logic |
| 50 | + default_status = 'invalid' |
| 51 | + elif error_code == 406: # Inactive recipient |
| 52 | + # All recipients were rejected as hard-bounce or spam-complaint. Email not sent. |
| 53 | + default_status = 'rejected' |
| 54 | + elif error_code == 0: |
| 55 | + # At least partial success, and email was sent. |
| 56 | + # Sadly, have to parse human-readable message to figure out if everyone got it. |
| 57 | + default_status = 'sent' |
| 58 | + rejected_emails = self.parse_inactive_recipients(msg) |
| 59 | + else: |
| 60 | + raise AnymailRequestsAPIError(email_message=message, payload=payload, response=response) |
| 61 | + |
| 62 | + return { |
| 63 | + recipient.email: AnymailRecipientStatus( |
| 64 | + message_id=message_id, |
| 65 | + status=('rejected' if recipient.email.lower() in rejected_emails |
| 66 | + else default_status) |
| 67 | + ) |
| 68 | + for recipient in payload.all_recipients |
| 69 | + } |
| 70 | + |
| 71 | + def parse_inactive_recipients(self, msg): |
| 72 | + """Return a list of 'inactive' email addresses from a Postmark "OK" response |
| 73 | +
|
| 74 | + :param str msg: the "Message" from the Postmark API response |
| 75 | + """ |
| 76 | + # Example msg with inactive recipients: |
| 77 | + # "Message OK, but will not deliver to these inactive addresses: [email protected], [email protected]." |
| 78 | + # " Inactive recipients are ones that have generated a hard bounce or a spam complaint." |
| 79 | + # Example msg with everything OK: "OK" |
| 80 | + match = re.search(r'inactive addresses:\s*(.*)\.\s*Inactive recipients', msg) |
| 81 | + if match: |
| 82 | + |
| 83 | + return [email.strip().lower() for email in emails.split(',')] |
| 84 | + else: |
| 85 | + return [] |
| 86 | + |
| 87 | + |
| 88 | +class PostmarkPayload(RequestsPayload): |
| 89 | + |
| 90 | + def __init__(self, message, defaults, backend, *args, **kwargs): |
| 91 | + headers = { |
| 92 | + 'Content-Type': 'application/json', |
| 93 | + 'Accept': 'application/json', |
| 94 | + # 'X-Postmark-Server-Token': see get_request_params (and set_esp_extra) |
| 95 | + } |
| 96 | + self.server_token = backend.server_token # added to headers later, so esp_extra can override |
| 97 | + self.all_recipients = [] # used for backend.parse_recipient_status |
| 98 | + super(PostmarkPayload, self).__init__(message, defaults, backend, headers=headers, *args, **kwargs) |
| 99 | + |
| 100 | + def get_api_endpoint(self): |
| 101 | + return "email" |
| 102 | + |
| 103 | + def get_request_params(self, api_url): |
| 104 | + params = super(PostmarkPayload, self).get_request_params(api_url) |
| 105 | + params['headers']['X-Postmark-Server-Token'] = self.server_token |
| 106 | + return params |
| 107 | + |
| 108 | + def serialize_data(self): |
| 109 | + return self.serialize_json(self.data) |
| 110 | + |
| 111 | + # |
| 112 | + # Payload construction |
| 113 | + # |
| 114 | + |
| 115 | + def init_payload(self): |
| 116 | + self.data = {} # becomes json |
| 117 | + |
| 118 | + def set_from_email(self, email): |
| 119 | + self.data["From"] = email.address |
| 120 | + |
| 121 | + def set_recipients(self, recipient_type, emails): |
| 122 | + assert recipient_type in ["to", "cc", "bcc"] |
| 123 | + if emails: |
| 124 | + field = recipient_type.capitalize() |
| 125 | + self.data[field] = ', '.join([email.address for email in emails]) |
| 126 | + self.all_recipients += emails # used for backend.parse_recipient_status |
| 127 | + |
| 128 | + def set_subject(self, subject): |
| 129 | + self.data["Subject"] = subject |
| 130 | + |
| 131 | + def set_reply_to(self, emails): |
| 132 | + if emails: |
| 133 | + reply_to = ", ".join([email.address for email in emails]) |
| 134 | + self.data["ReplyTo"] = reply_to |
| 135 | + |
| 136 | + def set_extra_headers(self, headers): |
| 137 | + self.data["Headers"] = [ |
| 138 | + {"Name": key, "Value": value} |
| 139 | + for key, value in headers.items() |
| 140 | + ] |
| 141 | + |
| 142 | + def set_text_body(self, body): |
| 143 | + self.data["TextBody"] = body |
| 144 | + |
| 145 | + def set_html_body(self, body): |
| 146 | + if "HtmlBody" in self.data: |
| 147 | + # second html body could show up through multiple alternatives, or html body + alternative |
| 148 | + self.unsupported_feature("multiple html parts") |
| 149 | + self.data["HtmlBody"] = body |
| 150 | + |
| 151 | + def make_attachment(self, attachment): |
| 152 | + """Returns Postmark attachment dict for attachment""" |
| 153 | + att = { |
| 154 | + "Name": attachment.name or "", |
| 155 | + "Content": attachment.b64content, |
| 156 | + "ContentType": attachment.mimetype, |
| 157 | + } |
| 158 | + if attachment.inline: |
| 159 | + att["ContentID"] = "cid:%s" % attachment.cid |
| 160 | + return att |
| 161 | + |
| 162 | + def set_attachments(self, attachments): |
| 163 | + if attachments: |
| 164 | + self.data["Attachments"] = [ |
| 165 | + self.make_attachment(attachment) for attachment in attachments |
| 166 | + ] |
| 167 | + |
| 168 | + # Postmark doesn't support metadata |
| 169 | + # def set_metadata(self, metadata): |
| 170 | + |
| 171 | + # Postmark doesn't support delayed sending |
| 172 | + # def set_send_at(self, send_at): |
| 173 | + |
| 174 | + def set_tags(self, tags): |
| 175 | + if len(tags) > 0: |
| 176 | + self.data["Tag"] = tags[0] |
| 177 | + if len(tags) > 1: |
| 178 | + self.unsupported_feature('multiple tags (%r)' % tags) |
| 179 | + |
| 180 | + # Postmark doesn't support click-tracking |
| 181 | + # def set_track_clicks(self, track_clicks): |
| 182 | + |
| 183 | + def set_track_opens(self, track_opens): |
| 184 | + self.data["TrackOpens"] = track_opens |
| 185 | + |
| 186 | + def set_esp_extra(self, extra): |
| 187 | + self.data.update(extra) |
| 188 | + # Special handling for 'server_token': |
| 189 | + self.server_token = self.data.pop('server_token', self.server_token) |
0 commit comments