![]() ![]() Next, we attached the message body to the email you can also attach HTML here depending on your need. The MIMEMultipart object accepts parameters in key-value pares therefore we sent the necessary mail parameters such as sender address, receiver address, and subject in the same manner. MIME (Multipurpose Internet Mail Extensions) Multipart is used when a mail has multiple messages or content having different content types such as Text, Image, HTML, or a file. Explanationįirst, the function creates a multipart message object. Since these variables include sensitive data I always recommend feeding this data from environment variables however you can also pass them as arguments to the function. FILE_NAME = Filename for the email attachment.PATH_TO_ZIP_FILE = Path to the zip file.Smtp_obj.sendmail(msg, msg, msg.as_string()) # Convert the message to a string and send it Smtp_obj.login(SMTP_USERNAME, SMTP_PASSWORD) Smtp_obj = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) Msg.attach(MIMEApplication(file.read(), Name='filename.zip')) # Attach the file with filename to the email With open(PATH_TO_ZIP_FILE,'rb') as file: Sending Zip File in an Email with Python from import MIMEMultipartįrom import MIMEApplicationīody_part = MIMEText(MESSAGE_BODY, 'plain') A simple google search will land you on multiple ways to get free SMTP servers. ![]() I am assuming that you already have an SMTP (Simple Mail Transfer Protocol ) server setup if not you can use Gmail SMTP or something like mailgun. In this tutorial, we will learn how to send emails with zip files using Python's built-in modules. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |