def send_email( to_email, subject, plaintext, html=None, unsubscribe_link=None, unsubscribe_via_email=False, ): if NOT_SEND_EMAIL: LOG.d( "send email with subject '%s' to '%s', plaintext: %s", subject, to_email, plaintext, ) return LOG.d("send email to %s, subject %s", to_email, subject) if POSTFIX_SUBMISSION_TLS: smtp = SMTP(POSTFIX_SERVER, 587) smtp.starttls() else: smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) msg = MIMEMultipart("alternative") msg.attach(MIMEText(plaintext)) if not html: LOG.d("Use plaintext as html") html = plaintext.replace("\n", "<br>") msg.attach(MIMEText(html, "html")) msg["Subject"] = subject msg["From"] = f"{SUPPORT_NAME} <{SUPPORT_EMAIL}>" msg["To"] = to_email msg_id_header = make_msgid() msg["Message-ID"] = msg_id_header date_header = formatdate() msg["Date"] = date_header if unsubscribe_link: add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>") if not unsubscribe_via_email: add_or_replace_header( msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click" ) # add DKIM email_domain = SUPPORT_EMAIL[SUPPORT_EMAIL.find("@") + 1 :] add_dkim_signature(msg, email_domain) msg_raw = to_bytes(msg) if SENDER: smtp.sendmail(SENDER, to_email, msg_raw) else: smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw)
def send_email(to_email, subject, plaintext, html=None, bounced_email: Optional[Message] = None): if NOT_SEND_EMAIL: LOG.d( "send email with subject %s to %s, plaintext: %s", subject, to_email, plaintext, ) return LOG.d("send email to %s, subject %s", to_email, subject) if POSTFIX_SUBMISSION_TLS: smtp = SMTP(POSTFIX_SERVER, 587) smtp.starttls() else: smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) if bounced_email: msg = MIMEMultipart("mixed") # add email main body body = MIMEMultipart("alternative") body.attach(MIMEText(plaintext, "text")) if html: body.attach(MIMEText(html, "html")) msg.attach(body) # add attachment rfcmessage = MIMEBase("message", "rfc822") rfcmessage.attach(bounced_email) msg.attach(rfcmessage) else: msg = MIMEMultipart("alternative") msg.attach(MIMEText(plaintext, "text")) if html: msg.attach(MIMEText(html, "html")) msg["Subject"] = subject msg["From"] = f"{SUPPORT_NAME} <{SUPPORT_EMAIL}>" msg["To"] = to_email msg_id_header = make_msgid() msg["Message-ID"] = msg_id_header date_header = formatdate() msg["Date"] = date_header # add DKIM email_domain = SUPPORT_EMAIL[SUPPORT_EMAIL.find("@") + 1:] add_dkim_signature(msg, email_domain) msg_raw = msg.as_bytes() smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw)
def send_email(to_email, subject, plaintext, html=None): if NOT_SEND_EMAIL: LOG.d( "send email with subject '%s' to '%s', plaintext: %s", subject, to_email, plaintext, ) return LOG.d("send email to %s, subject %s", to_email, subject) if POSTFIX_SUBMISSION_TLS: smtp = SMTP(POSTFIX_SERVER, 587) smtp.starttls() else: smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) msg = MIMEMultipart("alternative") msg.attach(MIMEText(plaintext, "text")) if not html: html = plaintext.replace("\n", "<br>") msg.attach(MIMEText(html, "html")) msg["Subject"] = subject msg["From"] = f"{SUPPORT_NAME} <{SUPPORT_EMAIL}>" msg["To"] = to_email msg_id_header = make_msgid() msg["Message-ID"] = msg_id_header date_header = formatdate() msg["Date"] = date_header # add DKIM email_domain = SUPPORT_EMAIL[SUPPORT_EMAIL.find("@") + 1:] add_dkim_signature(msg, email_domain) msg_raw = msg.as_bytes() if SENDER: smtp.sendmail(SENDER, to_email, msg_raw) else: smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw)
def send_email(to_email, subject, plaintext, html): if NOT_SEND_EMAIL: LOG.d( "send email with subject %s to %s, plaintext: %s", subject, to_email, plaintext, ) return # host IP, setup via Docker network smtp = SMTP(POSTFIX_SERVER, 25) msg = EmailMessage() msg["Subject"] = subject msg["From"] = f"Son from SimpleLogin <{SUPPORT_EMAIL}>" msg["To"] = to_email msg.set_content(plaintext) if html is not None: msg.add_alternative(html, subtype="html") msg_id_header = make_msgid() LOG.d("message-id %s", msg_id_header) msg["Message-ID"] = msg_id_header date_header = formatdate() LOG.d("Date header: %s", date_header) msg["Date"] = date_header # add DKIM email_domain = SUPPORT_EMAIL[SUPPORT_EMAIL.find("@") + 1:] add_dkim_signature(msg, email_domain) msg_raw = msg.as_string().encode() smtp.sendmail(SUPPORT_EMAIL, to_email, msg_raw)
def send_email( to_email, subject, plaintext, html=None, unsubscribe_link=None, unsubscribe_via_email=False, ): to_email = sanitize_email(to_email) if NOT_SEND_EMAIL: LOG.d( "send email with subject '%s' to '%s', plaintext: %s", subject, to_email, plaintext, ) return LOG.d("send email to %s, subject %s", to_email, subject) if POSTFIX_SUBMISSION_TLS: smtp = SMTP(POSTFIX_SERVER, 587) smtp.starttls() else: smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT or 25) msg = MIMEMultipart("alternative") msg.attach(MIMEText(plaintext)) if not html: LOG.d("Use plaintext as html") html = plaintext.replace("\n", "<br>") msg.attach(MIMEText(html, "html")) msg["Subject"] = subject msg["From"] = f"{SUPPORT_NAME} <{SUPPORT_EMAIL}>" msg["To"] = to_email msg_id_header = make_msgid() msg["Message-ID"] = msg_id_header date_header = formatdate() msg["Date"] = date_header if unsubscribe_link: add_or_replace_header(msg, "List-Unsubscribe", f"<{unsubscribe_link}>") if not unsubscribe_via_email: add_or_replace_header( msg, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click" ) # add DKIM email_domain = SUPPORT_EMAIL[SUPPORT_EMAIL.find("@") + 1 :] add_dkim_signature(msg, email_domain) msg_raw = to_bytes(msg) transaction = TransactionalEmail.get_by(email=to_email) if not transaction: transaction = TransactionalEmail.create(email=to_email, commit=True) # use a different envelope sender for each transactional email (aka VERP) smtp.sendmail(TRANSACTIONAL_BOUNCE_EMAIL.format(transaction.id), to_email, msg_raw)