Esempio n. 1
0
def test_generate_content(monkeypatch):
    monkeypatch.setattr(mistune, "Markdown", mock.MagicMock())
    monkeypatch.setattr(premailer, "transform", mock.MagicMock())
    monkeypatch.setattr(renderer, "HighlightRenderer", mock.MagicMock())
    monkeypatch.setattr(jinja2, "Template", mock.MagicMock())

    renderer.HighlightRenderer.return_value = 1
    premailer.transform.return_value = ""
    jinja2.Template.render.return_value = ""
    renderer.generate_content("")
    mistune.Markdown.assert_called_with(renderer=1)
Esempio n. 2
0
    def send(
        self,
        sender: str,
        subject: str,
        to: list,
        content: Optional[str] = None,
        file_path: Optional[str] = None,
        context: Optional[dict] = None,
        theme=None,
    ) -> None:

        if not context:
            context = {}

        if file_path:
            with open(file_path) as f:
                content = f.read()

        if content:
            html = renderer.generate_content(content,
                                             context=context,
                                             theme=theme)

            self.send_message(to, sender, html, content, subject)

        else:
            raise AttributeError(
                "You must provide either the content or filepath attribute")
Esempio n. 3
0
def send_message(
    sender: str,
    subject: str,
    to: list,
    content: Optional[str] = None,
    file_path: Optional[str] = None,
    context: Optional[dict] = None,
    theme=None,
):
    """
    Sends an email to a given list of recipients

    ### Parameters:

    - `sender`: the email address to send the message from. Must have been verified by SES
    - `subject`: The subject line of the email
    - `to`: A list of email addresses to send the email to
    - `content`: The content of the email to send. Either this parameter, or `file_path`, must be supplied
    - `file_path`: A local file path to a file to be send as the email body
    - `context`: Additional context to be sent to the email - can be inserted using Jinja2 template syntax
    - `theme`: A local file path to a css style sheet. If not supplied, the default style is used
    """
    if not context:
        context = {}

    if file_path:
        with open(file_path) as f:
            content = f.read()

    if content:
        message = generate_content(content, context=context, theme=theme)

        client = get_client()
        return client.send_email(
            Source=sender,
            Destination=dict(ToAddresses=to),
            Message=dict(
                Body=dict(
                    Html=dict(Charset="utf-8", Data=message),
                    Text=dict(Charset="utf-8", Data=content),
                ),
                Subject=dict(Charset="utf-8", Data=subject),
            ),
        )

    else:
        raise AttributeError(
            "You must provide either the content or filepath attribute")
Esempio n. 4
0
def send_message(
    sender: str,
    subject: str,
    to: list,
    content: Optional[str] = None,
    file_path: Optional[str] = None,
    context: Optional[dict] = None,
    theme=None,
):
    if not context:
        context = {}

    if all([content, file_path]) or not any([content, file_path]):
        raise AttributeError(
            "You must provide either the content or filepath attribute"
        )

    if file_path:
        with open(file_path) as f:
            content = f.read()

    kwargs = dict(md_content=content, context=context)

    if theme:
        kwargs["theme"] = theme

    message = generate_content(**kwargs)

    client = get_client()
    return client.send_email(
        Source=sender,
        Destination=dict(ToAddresses=to),
        Message=dict(
            Body=dict(
                Html=dict(Charset="utf-8", Data=message),
                Text=dict(Charset="utf-8", Data=content),
            ),
            Subject=dict(Charset="utf-8", Data=subject),
        ),
    )