Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
lib-py-b2b / email.py
Size: Mime:
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
        )