Пример #1
0
    def send(self):
        mail = Mail(
            from_email=self.from_email,
            to_emails=self.to_email,
            subject=self.title,
            html_content=self.content,
        )

        response = self.send_grid.client.mail.send.post(
            request_body=mail.get())
Пример #2
0
 def send(self,
          to_email=None,
          subject=None,
          content=None,
          content_type=None):
     from_email = Email(self.from_email)
     to_email = To(to_email)
     content = Content(content_type, content)
     mail = Mail(from_email, to_email, subject, content)
     return self.sendgrid_client.client.mail.send.post(
         request_body=mail.get())
Пример #3
0
def send_mail(hilights: List[Post]) -> None:
    emails = Email.query.all()

    if not hilights or not emails:
        return

    title_section = (
        f'<h2><a href="{settings.HOST_NAME}">Tarjoushaukka</a> bargain alerts</h2>'
    )
    hilight_messages_section = list(f"{hilight.content}<br/><br/>{hilight.url}"
                                    for hilight in hilights)
    unsubscribe_section = f'<p style="font-size:12px"><a href="{settings.HOST_NAME}?unsubscribe=-email-">Unsubscribe</a> from bargain alerts</p>'
    sections = [title_section
                ] + hilight_messages_section + [unsubscribe_section]
    message = "<br/><br/><br/>".join(sections)

    sg = sendgrid.SendGridAPIClient(api_key=settings.EMAIL_API_KEY)
    from_email = From(settings.FROM_EMAIL, "Tarjoushaukka")
    subject = "You have new bargain alerts!"
    content = Content("text/html", message)

    to_emails = [
        To(email=email.email, substitutions={"-email-": email.email})
        for email in emails
    ]

    mail = Mail(
        from_email=from_email,
        to_emails=to_emails,
        subject=subject,
        html_content=content,
        is_multiple=True,
    )
    try:
        print("SENDING MAIL...")
        print(emails)
        print(message)
        response = sg.client.mail.send.post(request_body=mail.get())
        print(response.status_code)
        print(response.body)
    except Exception as e:
        print(str(e))
Пример #4
0
def send_mail(hilights: Dict[str, List[Post]]) -> None:
    if not hilights:
        return

    for email, hilights in hilights.items():
        title_section = (
            f'<h2><a href="t{settings.HOST_NAME}">Ilmoitushaukka</a> sale alerts</h2>'
        )
        hilight_messages_section = list(
            f'<a href="{hilight.url}">{hilight.title}</a>'
            for hilight in hilights)
        unsubscribe_section = f'<p style="font-size:12px"><a href="{settings.HOST_NAME}?unsubscribe=-email-">Unsubscribe</a> from sale alerts</p>'
        message = f"{title_section}<br/>{'<br/><br/>'.join(hilight_messages_section)}<br/><br/><br/>{unsubscribe_section}"

        sg = SendGridAPIClient(api_key=settings.EMAIL_API_KEY)
        from_email = From(settings.FROM_EMAIL, "Ilmoitushaukka")
        to_email = To(email=email, substitutions={"-email-": email})
        subject = "You have new sale alerts!"
        content = Content("text/html", message)

        mail = Mail(
            from_email=from_email,
            to_emails=to_email,
            subject=subject,
            html_content=content,
            is_multiple=True,
        )
        try:
            print("SENDING MAIL...")
            print(email)
            print(message)
            response = sg.client.mail.send.post(request_body=mail.get())
            print(response.status_code)
            print(response.body)
        except Exception as e:
            print(str(e))