Repository URL to install this package:
|
Version:
0.4.184 ▾
|
lib-py-b2b
/
email.py
|
|---|
import boto3
class Email(object):
def __init__(self, to, subject, html=None, text=None):
self.to = to
self.subject = subject
self._html = html
self._text = text
self._format = 'html'
if not self._html:
self._format = 'text'
def html(self, html):
self._html = html
def text(self, text):
self._text = text
def send(self, from_addr=None):
if isinstance(self.to, str):
self.to = [self.to]
if not from_addr:
from_addr = 'no-reply@barnhardtapps.net'
if not self._html and not self._text:
raise Exception('You must provide a text or html body.')
client = boto3.client(
service_name = 'ses',
region_name = 'us-east-1'
)
message={
'Body': {},
'Subject': {
'Charset': 'UTF-8',
'Data': self.subject,
}
}
if self._format == 'html':
message['Body']['Html'] = {
'Charset': 'UTF-8',
'Data': self._html
}
else:
message['Body']['Text'] = {
'Charset': 'UTF-8',
'Data': self._text
}
return client.send_email(
Destination={
'ToAddresses': self.to
},
Message=message,
ReplyToAddresses=[from_addr],
Source=from_addr
)