def main(args): message = email.message.EmailMessage(email.policy.SMTP) message['To'] = 'Test Recipient <*****@*****.**>' message['From'] = 'Test Sender <*****@*****.**>' message['Subject'] = 'Foundations of Python Network Programming' message['Date'] = email.utils.formatdate(localtime=True) message['Message-ID'] = email.utils.make_msgid() if not args.i: message.set_content(html, subtype='html') message.add_alternative(plain) else: cid = email.utils.make_msgid() # RFC 2392: must be globally unique! message.set_content(html + img.format(cid.strip('<>')), subtype='html') message.add_related(blue_dot, 'image', 'gif', cid=cid, filename='blue-dot.gif') message.add_alternative(plain) for filename in args.filename: mime_type, encoding = mimetypes.guess_type(filename) if encoding or (mime_type is None): mime_type = 'application/octet-stream' main, sub = mime_type.split('/') if main == 'text': with open(filename, encoding='utf-8') as f: text = f.read() message.add_attachment(text, sub, filename=filename) else: with open(filename, 'rb') as f: data = f.read() message.add_attachment(data, main, sub, filename=filename) sys.stdout.buffer.write(message.as_bytes())
def generate(sender, recipient, subject, body, attachment_path): """Creates an email with an attachement.""" # Basic Email formatting message = email.message.EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) # Process the attachment and add it to the email if attachment_path != None: attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generate_email(sender, recipient, subject, body, attachment_path): # Create email with an attachment. message = email.message.EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) # Process attachment and add to email. attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype = mime_type, subtype = mime_subtype, filename = attachment_filename) return message
def generate_email(sender, recipient, subject, body, attachment_path=None): ''' Generates email with an attachment''' message = email.message.EmailMessage() message['From'] = sender message['To'] = recipient message['Subject'] = subject message.set_content(body) # Attachment if attachment_path: attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generate(sender, recipient, subject, body, attachment_path): """Creates an email with an attachement.""" # Basic Email formatting message = email.message.EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) # Making attachment_path optional, if the attachment variable is # empty string, no email will be sent. if not attachment_path == "": # Process the attachment and add it to the email attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generate(sender, recipient, subject, body, attachment_path): # generate the email """Creates an email with an attachment.""" # Basic Email formatting message = email.message.EmailMessage() # create message object message["From"] = sender # set From to sender message["To"] = recipient # set To to recipient message["Subject"] = subject # set Subject to subject message.set_content(body) # set the message body to body # Process the attachment and add it to the email attachment_filename = os.path.basename(attachment_path) # set attachment file mime_type, _ = mimetypes.guess_type(attachment_path) # automatically guess the mimetype mime_type, mime_subtype = mime_type.split('/', 1) # split and set the mimetype / subtype with open(attachment_path, 'rb') as ap: # open the attachment message.add_attachment(ap.read(), # read in the attachment maintype=mime_type, # set the mimetype subtype=mime_subtype, # set the mime subtype filename=attachment_filename) # give the attachment filename return message # returns the finished message
def generate(sender, recipient, subject, body, attachment_path): """create basic email with attachment""" # Basic Email information message = email.message.EmailMessage() message['From'] = sender message['To'] = recipient message['Subject'] = subject message.set_content(body) # Process the attachment and add it to the email attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generateEmail(sender, recipient, subject, body, attachment_path): #Basic Email formatting message = email.message.EmailMessage() message['From'] = sender message['To'] = recipient message['Subject'] = subject message.set_content(body) #Process the attachment if any and add it to mail if not attachment_path == "": attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generate_email(recipient, sender="*****@*****.**", subject=default_subject, body=default_body, attachment_path="/tmp/processed.pdf"): """Generate an email """ message = email.message.EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) #Add attachment (pdf report) attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) return message
def generate_email(sender, recipient, subject="", body="", attachment_path=""): """Creates an email with an attachement.""" # Basic Email formatting message = email.message.EmailMessage() message["From"] = sender message["To"] = recipient message["Subject"] = subject message.set_content(body) # Add attachment if she exist if attachment_path: # Process the attachment and add it to the email attachment_file = Path(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_file.name) return message
def generate_email(msg_sender, msg_recipient, msg_subject, msg_body, attachment_path=None): """Generate email, default is with no attachment""" # Basic Email formatting message = email.message.EmailMessage() message['Subject'] = msg_subject message['From'] = msg_sender message['To'] = msg_recipient message.set_content(msg_body) if attachment_path != None: attachment_name = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split("/", 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_name) return message
def main(args): message = email.message.EmailMessage(email.policy.SMTP) message['To'] = 'Test Recipient <*****@*****.**>' message['From'] = 'Test Sender <*****@*****.**>' message['Subject'] = 'Foundations of Python Networking Programming' message['Date'] = email.utils.formatdate(localtime=True) message['Message-ID'] = email.utils.make_msgid() if not args.i: message.set_content(html, subtype='html') message.add_alternative(plain) else: # RFC 2392: must be globally unique! cid = email.utils.make_msgid() message.set_content(html + img.format(cid.strip('<>')), subtype='html') message.add_related(blue_dot, 'image', 'gif', cid=cid, filename='blue-dot.gif') message.add_alternative(plain) for filename in args.filename: mime_type, encoding = mimetypes.guess_type(filename) if encoding or (mime_type is None): mime_type = 'application/octet-stream' main, sub = mime_type.split('/') if main == 'text': with open(filename, encoding='utf-8') as f: text = f.read() message.add_attachment(text, sub, filename=filename) else: with open(filename, 'rb') as f: data = f.read() message.add_attachment(data, main, sub, filename=filename) sys.stdout.buffer.write(message.as_bytes())
def generate(sender, recipient, subject, body, attachment_path=False): """ This function receives 4 mandatory and 1 optional arguments Example: >> mandatory: sender = gerardo.ocampos recipient = juan.perez subject = 'A simple subject' body = 'content of the mail' >> optional: attachment_path = '/tmp/example.pdf' generate('gerardo.ocampos','juan.perez','A simple subject', 'content of the mail', '/tmo/example.pdf') RETURN: message object """ message = email.message.EmailMessage() message['From'] = sender message['To'] = recipient message['Subject'] = subject message.set_content(body) if attachment_path: attachment_file = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_file) return message
def create(item, codec=Codec): """Create an email.message.EmailMessage object using given codec """ message = email.message.EmailMessage() identifier = codec.get_id(item) issuer = codec.get_issuer(item) sender, domain = email.utils.parseaddr(issuer)[1].split("@") message.add_header("From", issuer) message.add_header("To", codec.get_recipient(item)) message.add_header("Message-ID", "<{}@{}>".format(uuid.uuid4(), domain)) message.add_header("References", "<{}@{}>".format(identifier, domain)) message.add_header("Reply-To", "{}+{}@{}".format(sender, identifier, domain)) message.add_header( "Subject", "".join( "[{}]".format(t) for t in [codec.get_human_id(item), codec.get_external_id(item)] if t) + " " + codec.get_subject(item), ) context = codec.get_context(item) plain = codec.render_template(codec.get_text_template(item), context) message.set_content(plain) message.add_alternative( json.dumps(context).encode("utf-8"), "application", "json") message.add_alternative( codec.render_template( codec.get_html_template(item), { "markup": codec.text_to_html(plain), "context": context }, ), "html", ) for file_ in codec.get_attachments(item) or []: filename = os.path.basename(file_.name) maintype, subtype = mimetypes.guess_type(filename)[0].split("/", 1) content = file_.read() if maintype == "text": charset = "utf-8" try: content = content.decode(charset) except UnicodeDecodeError: charset = chardet.detect(content).get("encoding") try: content = content.decode(charset) except UnicodeDecodeError: maintype, subtype = "application", "octet-stream" if maintype == "text": message.add_attachment(content, maintype, charset=charset, filename=filename) else: message.add_attachment(content, maintype, subtype, filename=filename) return message
## Generate Message message = email.message.EmailMessage() message["From"] = sender message["To"] = receiver message["Subject"] = subject message.set_content(body) # Process the attachment and add it to the email attachment_filename = os.path.basename(attachment_path) mime_type, _ = mimetypes.guess_type(attachment_path) mime_type, mime_subtype = mime_type.split('/', 1) with open(attachment_path, 'rb') as ap: message.add_attachment(ap.read(), maintype=mime_type, subtype=mime_subtype, filename=attachment_filename) ## Send Message mail_server = smtplib.SMTP('localhost') mail_server.send_message(message) mail_server.quit() ## Formal alternativa para enviarlo con emails y no con email # import emails # if __name__ == "__main__": # path = "supplier-data/descriptions/" # title = "Process Updated on " + current_date # #generate the package for pdf body # package = generate_pdf(path) # reports.generate_report("/tmp/processed.pdf", title, package)