def _send_mail( email_cfg: EmailConfig, recipients: typing.Iterable[str], mail_template: str, subject: str, replace_tokens: dict={}, cc_recipients: typing.Iterable[str]=[], mimetype='text', ): not_none(email_cfg) not_empty(recipients) not_none(mail_template) not_empty(subject) # create body from template mail_body = mailer.create_body( mail_template=mail_template, replace_tokens=replace_tokens, ) recipients = {r.lower() for r in recipients} cc_recipients = {r.lower() for r in cc_recipients} sender_name = email_cfg.sender_name() # used for the header sender = sender_name # passed as from-address to the mailserver if email_cfg.use_tls(): smtp_server = smtplib.SMTP_SSL(email_cfg.smtp_host()) else: smtp_server = smtplib.SMTP(email_cfg.smtp_host()) if email_cfg.has_credentials(): credentials = email_cfg.credentials() sender = credentials.username() sender_name = email_cfg.sender_name() or sender smtp_server.login(user=credentials.username(), password=credentials.passwd()) # create mail envelope mail = mailer.create_mail( subject=subject, sender=sender_name, recipients=recipients, cc_recipients=cc_recipients, text=mail_body, mimetype=mimetype, ) recipients.update(cc_recipients) mailer.send_mail( smtp_server=smtp_server, msg=mail, sender=sender, recipients=recipients )
def _send_mail( email_cfg: EmailConfig, recipients: typing.Iterable[str], mail_template: str, subject: str, attachments: typing.Sequence[mail.model.Attachment] = (), replace_tokens: dict = {}, cc_recipients: typing.Iterable[str] = [], mimetype='text', ): not_none(email_cfg) not_empty(recipients) not_none(mail_template) not_empty(subject) # create body from template mail_body = mailer.create_body( mail_template=mail_template, replace_tokens=replace_tokens, ) recipients = {r.lower() for r in recipients} cc_recipients = {r.lower() for r in cc_recipients} sender_name = email_cfg.sender_name() if email_cfg.use_tls(): smtp_server = smtplib.SMTP_SSL(email_cfg.smtp_host()) else: smtp_server = smtplib.SMTP(email_cfg.smtp_host()) if email_cfg.has_credentials(): credentials = email_cfg.credentials() smtp_server.login(user=credentials.username(), password=credentials.passwd()) # create mail envelope mail = mailer.create_mail( subject=subject, sender=sender_name, recipients=recipients, cc_recipients=cc_recipients, text=mail_body, mimetype=mimetype, attachments=attachments, ) recipients.update(cc_recipients) recipients = email_cfg.filter_recipients(recipients) smtp_server.send_message( msg=mail, to_addrs=recipients) # from_addr is taken from header
def _send_mail( email_cfg: EmailConfig, recipients: typing.Iterable[str], mail_template: str, subject: str, replace_tokens: dict = {}, cc_recipients: typing.Iterable[str] = [], mimetype='text', ): not_none(email_cfg) not_empty(recipients) not_none(mail_template) not_empty(subject) # create body from template mail_body = mailer.create_body( mail_template=mail_template, replace_tokens=replace_tokens, ) recipients = set(map(str.lower, recipients)) cc_recipients = set(map(str.lower, cc_recipients)) # create mail envelope mail = mailer.create_mail( subject=subject, sender=email_cfg.sender_name(), recipients=recipients, cc_recipients=cc_recipients, text=mail_body, mimetype=mimetype, ) if email_cfg.use_tls(): smtp_server = smtplib.SMTP_SSL(email_cfg.smtp_host()) else: smtp_server = smtplib.SMTP(email_cfg.smtp_host()) credentials = email_cfg.credentials() smtp_server.login(user=credentials.username(), password=credentials.passwd()) recipients.update(cc_recipients) mailer.send_mail(smtp_server=smtp_server, msg=mail, sender=credentials.username(), recipients=recipients)