~linuxgoose/Inque

ref: 24ccd76b032a72282e5a32ba4d892b57c552b4b3 Inque/inque/tickets/mail_sender.py -rw-r--r-- 1.2 KiB
24ccd76bJordan Robinson remove duplicate import 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import smtplib
from email.mime.text import MIMEText
from inque.settings import SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, FROM_EMAIL, SUPPORT_EMAIL, SUPPORT_COMPANY
from tickets.utils import get_message_id, generate_message_id
from django.conf import settings
from tickets.models import Message

def send_email(to_email, subject, body, ticket, sender_email, in_reply_to=None, references=None, reply_to=None):
    msg = MIMEText(body, "plain")
    msg["Subject"] = subject
    msg["From"] = f"{SUPPORT_COMPANY} Support <{SUPPORT_EMAIL}>"  # Display name + mailbox
    msg["Reply-To"] = SUPPORT_EMAIL

    # Determine recipients
    if to_email == '':
        if sender_email == ticket.reporter:
            recipients = ticket.assignee if ticket.assignee else settings.SUPPORT_AGENTS
        else:
            recipients = [ticket.reporter]
    else:
        recipients = [to_email]
    msg["To"] = ", ".join(recipients)

    # Set references and message ids
    if in_reply_to:
        msg["In-Reply-To"] = in_reply_to
    if references:
        msg["References"] = references

    with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
        server.starttls()
        server.login(SMTP_USER, SMTP_PASSWORD)
        server.sendmail(SUPPORT_EMAIL, recipients, msg.as_string())