def send_autoreply(sender, mailbox, armessage, original_msg):
    """Send an autoreply message."""
    if armessage.fromdate > timezone.now():
        return

    if armessage.untildate is not None \
            and armessage.untildate < timezone.now():
        armessage.enabled = False
        armessage.save()
        return

    try:
        lastar = ARhistoric.objects.get(armessage=armessage.id, sender=sender)
        timeout = parameters.get_admin("AUTOREPLIES_TIMEOUT",
                                       app="modoboa_postfix_autoreply")
        delta = datetime.timedelta(seconds=int(timeout))
        now = timezone.make_aware(datetime.datetime.now(),
                                  timezone.get_default_timezone())
        if lastar.last_sent + delta > now:
            logger.debug(
                "no autoreply message sent because delta (%s) < timetout (%s)",
                delta, timeout
            )
            sys.exit(0)

    except ARhistoric.DoesNotExist:
        lastar = ARhistoric()
        lastar.armessage = armessage
        lastar.sender = sender

    msg = MIMEText(armessage.content.encode("utf-8"), _charset="utf-8")
    set_email_headers(
        msg, "{} Re: {}".format(armessage.subject, original_msg["Subject"]),
        mailbox.user.encoded_address, sender
    )
    msg["Auto-Submitted"] = "auto-replied"
    msg["Precedence"] = "bulk"
    message_id = original_msg.get("Message-ID")
    if message_id:
        msg["In-Reply-To"] = message_id
        msg["References"] = message_id

    try:
        s = smtplib.SMTP("localhost")
        s.sendmail(mailbox.user.encoded_address, [sender], msg.as_string())
        s.quit()
    except smtplib.SMTPException as exp:
        logger.error("Failed to send autoreply message: %s", exp)
        sys.exit(1)

    logger.debug(
        "autoreply message sent to %s" % mailbox.user.encoded_address)

    lastar.last_sent = datetime.datetime.now()
    lastar.save()
def send_autoreply(sender, mailbox, armessage, original_msg):
    """Send an autoreply message."""
    if armessage.fromdate > timezone.now():
        return

    if armessage.untildate is not None \
            and armessage.untildate < timezone.now():
        armessage.enabled = False
        armessage.save()
        return

    try:
        lastar = ARhistoric.objects.get(armessage=armessage.id, sender=sender)
        timeout = parameters.get_admin("AUTOREPLIES_TIMEOUT",
                                       app="modoboa_postfix_autoreply")
        delta = datetime.timedelta(seconds=int(timeout))
        now = timezone.make_aware(datetime.datetime.now(),
                                  timezone.get_default_timezone())
        if lastar.last_sent + delta > now:
            logger.debug(
                "no autoreply message sent because delta (%s) < timetout (%s)",
                delta, timeout)
            sys.exit(0)

    except ARhistoric.DoesNotExist:
        lastar = ARhistoric()
        lastar.armessage = armessage
        lastar.sender = sender

    msg = MIMEText(armessage.content.encode("utf-8"), _charset="utf-8")
    set_email_headers(
        msg, "{} Re: {}".format(armessage.subject, original_msg["Subject"]),
        mailbox.user.encoded_address, sender)
    msg["Auto-Submitted"] = "auto-replied"
    msg["Precedence"] = "bulk"
    message_id = original_msg.get("Message-ID")
    if message_id:
        msg["In-Reply-To"] = message_id
        msg["References"] = message_id

    try:
        s = smtplib.SMTP("localhost")
        s.sendmail(mailbox.user.encoded_address, [sender], msg.as_string())
        s.quit()
    except smtplib.SMTPException as exp:
        logger.error("Failed to send autoreply message: %s", exp)
        sys.exit(1)

    logger.debug("autoreply message sent to %s" % mailbox.user.encoded_address)

    lastar.last_sent = datetime.datetime.now()
    lastar.save()
Esempio n. 3
0
    def to_msg(self, request):
        """Convert form's content to an object ready to send.

        We set headers at the end to be sure no one will override
        them.

        """
        msg = self._build_msg(request)
        set_email_headers(msg, self.cleaned_data["subject"],
                          request.user.encoded_address,
                          self.cleaned_data['to'])
        origmsgid = self.cleaned_data.get("origmsgid", None)
        if origmsgid:
            msg["References"] = msg["In-Reply-To"] = origmsgid
        return msg
Esempio n. 4
0
    def to_msg(self, request):
        """Convert form's content to an object ready to send.

        We set headers at the end to be sure no one will override
        them.

        """
        msg = self._build_msg(request)
        set_email_headers(
            msg, self.cleaned_data["subject"], request.user.encoded_address,
            self.cleaned_data['to']
        )
        origmsgid = self.cleaned_data.get("origmsgid", None)
        if origmsgid:
            msg["References"] = msg["In-Reply-To"] = origmsgid
        return msg