Python Email Script
by - Thursday, January 1, 1970 at 12:00 AM
Found this script to send emails via a Python script and edited it a bit to be able to send multiple messages as opposed to just one by using a for loop at the end. Worked with just one, adding the for loop broke it:(

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

MY_ADDRESS = ""
MY_PASSWORD = ""
RECIPIENT_ADDRESS = ""

HOST_ADDRESS = ""
HOST_PORT =

#connection to server
server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
server.starttls()
server.login(MY_ADDRESS, MY_PASSWORD)

#create mime multipart object
message = MIMEMultipart()

#mime multipart object header
message['From'] = MY_ADDRESS
message['To'] = RECIPIENT_ADDRESS
message['Subject'] = "Automated Email with Attachment"

#mime text
textPart = MIMEText("Did it work?", 'plain')

#mime application
filename = "document.txt"
filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

#attach parts
message.attach(textPart)
message.attach(filePart)

#send message as many times as needed (this is what breaks it)
for i in range(5):
    server.send_message(message)
Reply
let try this
Reply
Thank you, Lemme try this out
Reply
you need to sleep the program in every loop iteration!
Reply
(October 1, 2022, 09:42 PM)lkajsdaed Wrote: you need to sleep the program in every loop iteration!


fukn genius :heart:
Reply
(October 1, 2022, 09:42 PM)lkajsdaed Wrote: you need to sleep the program in every loop iteration!

Made it into a function and just call it and the end. Still didnt work. Pain

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

MY_ADDRESS = ""
MY_PASSWORD = ""
RECIPIENT_ADDRESS = ""

HOST_ADDRESS = ""
HOST_PORT =

def sendMail():
    #connection to server
    server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
    server.starttls()
    server.login(MY_ADDRESS, MY_PASSWORD)

    #create mime multipart object
    message = MIMEMultipart()

    #mime multipart object header
    message['From'] = MY_ADDRESS
    message['To'] = RECIPIENT_ADDRESS
    message['Subject'] = "Automated Email with Attachment"

    #mime text
    textPart = MIMEText("Did it work?", 'plain')

    #mime application
    filename = "document.txt"
    filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
    filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

    #attach parts
    message.attach(textPart)
    message.attach(filePart)

    #send message as many times as needed

    server.send_message(message)
    server.close()

sendMail()
Reply
awesome!
Reply
Awesome I will use this !
Reply
nice work!
Reply
awesome tnx
Reply


 Users viewing this thread: Python Email Script: No users currently viewing.