def send_accepted_mail(request, notice): """ Sends a mail to the publisher with the contents of the notice. We use named temporary files because the files stored by depot does not have a nice filename (which is automatically used by mailton) and to allow temporary files from the memory without a real file on the disk. """ def construct_subject(notice, request): issues = notice.issue_objects number = issues[0].number if issues else '' organization = notice.organization_object parent = organization.parent if organization else None parent_id = (parent.external_name or '') if parent else '' prefixes = [] if notice.at_cost: prefixes.append(request.translate(_("With costs"))) if notice.print_only: prefixes.append(request.translate(_("Print only"))) prefix = '' if not prefixes else "{} - ".format(" / ".join(prefixes)) return "{}{} {} {} {}".format(prefix, number, parent_id, notice.title, notice.id) reply_to = (request.app.principal.on_accept.get('mail_from') or request.app.mail['transactional']['sender']) subject = construct_subject(notice, request) content = render_template('mail_on_accept.pt', request, { 'title': subject, 'model': notice, 'layout': MailLayout(notice, request) }) attachments = [] for file in notice.files: attachment = Attachment(file.reference.file._file_path) content_disposition = 'attachment; filename="{}"'.format(file.name) attachment.headers['Content-Disposition'] = content_disposition attachments.append(attachment) request.app.send_transactional_email( subject=subject, receivers=(request.app.principal.on_accept['mail_to'], ), reply_to=reply_to, content=content, attachments=attachments)
def email(sender=None, receivers=(), cc=(), bcc=(), subject=None, content=None, encoding='utf8', attachments=()): """ Creates a Collection object with a HTML *content*, and *attachments*. :param content: HTML content. :param encoding: Encoding of the email. :param attachments: List of filenames to attach to the email. """ enclosure = [HTML(content, encoding)] enclosure.extend(Attachment(k) for k in attachments) return Collection(*enclosure, headers=[ headers.subject(subject), headers.sender(sender), headers.to(*receivers), headers.cc(*cc), headers.bcc(*bcc), headers.date(), headers.message_id(), ])
def test_headers_priority(self): a = Attachment('tests/assets/spacer.gif', headers={'Content-Disposition': 'something'}) mime = mimetest(a.mime()) assert mime['Content-Disposition'] == 'something'
def enclosure(self): raw = Attachment('tests/assets/spacer.gif', headers=self.headers) return raw