def _create_text_attachment(self, file_content: str) -> mail_helpers.Attachment: encoded_content = base64.b64encode(file_content.encode("ascii")).decode() return mail_helpers.Attachment( file_content=mail_helpers.FileContent(encoded_content), file_name=mail_helpers.FileName( "Recidiviz Monthly Report - Client Details.txt" ), file_type=mail_helpers.FileType("text/plain"), disposition=mail_helpers.Disposition("attachment"), )
def _create_text_attachment( self, file_content: str, attachment_title: Optional[str] = None, ) -> mail_helpers.Attachment: encoded_content = base64.b64encode( file_content.encode("ascii")).decode() return mail_helpers.Attachment( file_content=mail_helpers.FileContent(encoded_content), file_name=mail_helpers.FileName(attachment_title), file_type=mail_helpers.FileType("text/plain"), disposition=mail_helpers.Disposition("attachment"), )
def PrepAttachment(attach_file_address): with open(attach_file_address, 'rb') as f: data = f.read() encoded = base64.b64encode(data).decode() attachment = sgm.Attachment() attachment.file_content = sgm.FileContent(encoded) if attach_file_address[-4:].lower() in ['jpeg', '.png', '.jpg', '.gif']: attachment.file_type = sgm.FileType("image/jpeg") else: attachment.file_type = sgm.FileType("application/pdf") attachment.file_name = sgm.FileName(os.path.basename(attach_file_address)) attachment.disposition = sgm.Disposition("attachment") return attachment
def add_attachment(self, f): tmp_dir = TemporaryDirectory() path = os.path.join(tmp_dir.name, 'challan_report.pdf') with open(path, 'wb+') as des: for chunk in f.chunks(): des.write(chunk) with open(path, 'rb') as f: data = f.read() encoded = base64.b64encode(data).decode() a = mail.Attachment() a.file_content = mail.FileContent(encoded) a.file_type = mail.FileType('application/pdf') a.file_name = mail.FileName('challan_report.pdf') a.disposition = mail.Disposition('attachment') a.content_id = mail.ContentId('Example Content ID') self.attachment = a
def create_email( self, to_list: List[str], subject: str, html_content: str, image: bytes = None, content_type: str = None, send_at: datetime = None, ) -> mail.Mail: """ Create a new sendgrid email object. Params: - to_list: List[str] - The recipients list. - subject: str - The email subject. - html_content: str - HTML text to fill the email. - image: bytes - A optional image to attachment in email. - content_type: str - The content type of the image. - send_at: datetime - The datetime when the email must be sended. Return: - message: Mail - The sendgrid email object. """ message = mail.Mail() message.from_email = mail.From(self.from_email) message.subject = mail.Subject(subject) _users_list = [] for _to in to_list: _users_list.append(mail.To(_to)) message.to = _users_list if image: ext = str(content_type).split("/")[1] timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H%M%S") message.attachment = mail.Attachment( mail.FileContent(image), mail.FileName(f"event_image-{timestamp}.{ext}"), mail.FileType(str(content_type)), mail.Disposition("attachment"), ) if send_at: message.send_at = mail.SendAt(self.get_unix_time(send_at), p=0) message.content = mail.Content(mail.MimeType.html, html_content) return message