예제 #1
0
    def post(self, request, format=None):
        data = json.loads(request.body)
        lesson = data['lesson']
        account = data['account']
        print("lesson : %s" % lesson)
        print("account : %s" % account['email'])
        mail = EmailMultiAlternatives(
            subject="Your Subject",
            body="This is a simple text email body.",
            from_email="*****@*****.**",
            to=["*****@*****.**"],
            headers={"Reply-To": "*****@*****.**"})
        # Add template
        mail.template_id = 'f6581133-5c0b-4cc1-9a86-96a8bcd0db06'

        # Replace substitutions in sendgrid template
        mail.substitutions = {
            '%type%': 'Hatha',
            '%date%': 'mercredi 28 juin',
            '%heure%': '11h30'
        }

        # Add categories
        mail.categories = [
            'confirmation',
        ]

        mail.send()
        return Response({
            'status': 'OK',
            'message': 'Email sent'
        },
                        status=status.HTTP_200_OK)
예제 #2
0
def send_mail(
    subject: str,
    html_body: str,
    from_email: str,
    to: List[str],
    categories: List[str] = None,
    reply_to: str = None,
    headers: dict = None,
    preheader: str = None,
    template_id: str = None,
):
    """Send mail using the dynamic template specified by template_id.

    This template should include these variables:
    - Subject: {{ subject }}
    - Preheader: {{ preheader }}
    - Somewhere in the template: {{{ body }}}

    Tip: to display a name for the from_email, use this format:
    - `"Name" <*****@*****.**>`
    """
    if headers is None:
        headers = dict()
    headers["Reply-To"] = reply_to

    raw_body = strip_tags(html_body)
    mail = EmailMultiAlternatives(subject=subject,
                                  body=raw_body,
                                  from_email=from_email,
                                  to=to,
                                  headers=headers)
    if not template_id:
        template_id = settings.TEMPLATE_ID

    mail.template_id = template_id
    mail.dynamic_template_data = dict(body=html_body,
                                      subject=subject,
                                      preheader=preheader)
    if categories:
        mail.categories = categories

    mail.send()
예제 #3
0
    def post(self, request, format=None):
        data = json.loads(request.body)

        type = data['type']
        name = data['name']
        email = data['email']
        tel = data['tel']
        message = data['message']

        mail = EmailMultiAlternatives(
            subject="Subject",
            body="Body",
            from_email="*****@*****.**",
            to=["*****@*****.**"],
            headers={"Reply-To": "*****@*****.**"})

        # Add template (ContactMessage)
        mail.template_id = '4f9c3e44-7d6f-4ff8-a8f2-99018e998aff'

        # Replace substitutions in sendgrid template
        mail.substitutions = {
            '%type%': type,
            '%email%': email,
            '%nom%': name,
            '%message%': message,
            '%tel%': tel
        }

        # Add categories
        mail.categories = [
            'contact',
        ]

        mail.send()

        return Response({
            'status': 'OK',
            'message': 'Email sent'
        },
                        status=status.HTTP_200_OK)
예제 #4
0
def send_html_mail(template_name,
                   context=None,
                   from_email=settings.DEFAULT_FROM_EMAIL,
                   to=None,
                   bcc=None,
                   cc=None,
                   reply_to=None,
                   headers=None):
  """Sends email rendered via templates.

    If `from_email` is a list of user instances or a single one,
    it will build a custom formatting for them using the stored data
    "Full Name <*****@*****.**>".

    This allows us to keep base email templates and just edit the parts of
    the body or subject.

    Also we're using a single template for all related notifications.
    Both email subject/body are saved in the same template.

    Params:
        template_name: Relative template path. See 'notifications/base.html' for the
            block names to use.
        context: Dictionary containing values to plug into the template
        from_email: String, containing email address on behalf of whom this
                    email is sent. Provided by default in settings.
        to: a list of email addresses or User instances, for the TO field.
        bcc: a list of email addresses, for the BCC field
        cc: a list of email addresses, for the CC field
        headers: Eg. {"Reply-To": "*****@*****.**"}
    """
  # Set default values
  to = to or []
  cc = cc or []
  bcc = bcc or []
  context = context or {}
  context.update(**settings_constants())
  headers = headers or {}

  # Format email address with names if User instances
  if isinstance(to, (list, QuerySet)):
    to = [format_user_email(user) for user in to]
  else:
    to = [format_user_email(to)]

  if reply_to:
    headers["Reply-To"] = reply_to

  # Rendering subject
  context["render_subject"] = True
  subject = render_to_string(template_name, context)
  subject = subject.replace("\r", " ").replace("\n", " ").strip()
  subject = "".join(subject.splitlines())

  # Rendering body(both html + text version)
  context["render_subject"] = False
  html_version = render_to_string(template_name, context).strip()
  text_version = html2text.html2text(html_version)
  message = EmailMultiAlternatives(
      subject=subject,
      body=text_version,
      from_email=from_email,
      to=to,
      cc=cc,
      bcc=bcc,
      headers=headers)
  message.attach_alternative(html_version, "text/html")

  # Adds template_name as category for sendgrid,
  # so we can group stats by category
  message.categories = [template_name]

  return message.send()