def _wrap_message(milter): attachment = MIMEPart(policy=SMTPUTF8) attachment.set_content(milter.msg.as_bytes(), maintype="plain", subtype="text", disposition="attachment", filename=f"{milter.qid}.eml", params={"name": f"{milter.qid}.eml"}) milter.msg.clear_content() milter.msg.set_content( "Please see the original email attached.") milter.msg.add_alternative( "<html><body>Please see the original email attached.</body></html>", subtype="html") milter.msg.make_mixed() milter.msg.attach(attachment)
def attach_stream(self, stream: IOBase, file_name: str) -> Message: """Read a stream into an attachment and attach to this message. This method returns the object, so you can chain it like:: msg.attach(file_handle, "test.txt").attach(byte_stream, "test.bin") Args: stream (IOBase): The stream to read from. file_name (str): The name of the file, used for MIME type identification. Returns: Message: this Message, for chaining. """ mime_type = mimetypes.guess_type(file_name)[0] # it's possible we get a file that doesn't have a mime type, like a # Linux executable, or a mach-o file - in that case just set it # to octet-stream as a generic stream of bytes if mime_type is None: main_type, sub_type = ("application", "octet-stream") else: main_type, sub_type = mime_type.split("/") attachment = MIMEPart() # we need special handling for set_content with datatype of str, as # for some reason this method doesn't like 'maintype' # see: https://docs.python.org/3/library/ # email.contentmanager.html#email.contentmanager.set_content content_args = {"subtype": sub_type} if main_type != "text": content_args["maintype"] = main_type file_name = path.basename(file_name) attachment.set_content(stream.read(), filename=file_name, disposition="attachment", **content_args) self.attachments.append(attachment) return self
def new(request): if request.method == "GET": # test data data = { "subject": "testing from outlook", "sender": "*****@*****.**", } form = EmailForm(data) return render(request, "mails/new.html", context={"form": form}) elif request.method == "POST": # getting textual data from FORM data = request.POST # getting files data from FORM files = request.FILES.getlist("attachments") from_email = data["sender"] receipients = [e.strip() for e in data["receiver"].split("\n")] subject = data["subject"] content = data["content"] content_html_image_encoded = parse_images_to_base64(content) # creating msg object part by part msg = EmailMessage() msg["From"] = from_email msg["Subject"] = subject msg.make_mixed() # content html_part = MIMEPart() html_part.set_content(content_html_image_encoded, subtype="html") msg.attach(html_part) # attachment if len(files) > 0: for f in files: maintype, subtype = f.content_type.split("/") msg.add_attachment( f.read(), maintype=maintype, subtype=subtype, filename=f.name, disposition="attachment", ) with smtplib.SMTP("smtp.office365.com", 587) as server: server.ehlo() server.starttls() server.login(data['outlook_email'], data['outlook_password']) for to in receipients: msg['To'] = to server.send_message(msg) del msg['To'] # creating bulkmail and save to db data = { "author": request.user, "sender": from_email, "receiver": receipients, "subject": subject, "content": content, "date": timezone.now(), } this_bulk = BulkEmail(**data) this_bulk.save() messages.add_message(request, messages.SUCCESS, "Mail Sent!") return redirect("profile")