Simple e-mail sender, that automatically changes subject for every e-mail - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Simple e-mail sender, that automatically changes subject for every e-mail (/thread-4541.html) |
Simple e-mail sender, that automatically changes subject for every e-mail - joker - Aug-24-2017 Hello! I am new to python. I found this code, and it works good, but I want to make a small change - I want that when I input subject, it automatically adds number 1 in the end of the subject, and for every new e-mail send, increments added number by value of one. I tried everything I can find, but it always crashes. May be someone can pinpoint the right approach? For example, if I enter in subject line "Test", and set amount to 3 e-mails, it should send 3 e-mails with subjects: Test 1 Test 2 Test 3 The code I have so far. print("Loading...") from time import strftime, localtime, sleep import os import smtplib def senda(msg, subject, to, amount, frm, password): import time import sys import smtplib num = 0 left = amount server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() global successful successful = 0 body = 'Subject: %s\n%s' % (subject, msg) try: server.login(frm, password) except: from time import sleep print("Error logging in.") sleep(5) import sys sys.exit() else: for i in range(amount): try: server.sendmail(frm, [to], body) except: pass else: successful += 1 print("%d emails sent" % successful) while True: time = strftime("%Y-%m-%d %H:%M:%S", localtime()) user_email = input("Email: ") user_pass = input("Password: ") to = input("To: ") subject = input("Subject: ") message = '\n' + input("Message: ") while True: amt = input("Amount: ") try: amt = int(amt) except: pass else: break senda(message, subject, to, amt, user_email, user_pass)For all the answers, thanks in advance! :) RE: Simple e-mail sender, that automatically changes subject for every e-mail - wavic - Aug-24-2017 Move line 15 body = 'Subject: %s\n%s' % (subject, msg) under line 26. Change it to looks like this:for i in range(1, amount): try: subject = "test {}".format(str(i)) body = 'Subject: %s\n%s' % (subject, msg) server.sendmail(frm, [to], body) except: pass else: successful += 1 print("%d emails sent" % successful) RE: Simple e-mail sender, that automatically changes subject for every e-mail - joker - Aug-24-2017 This is exactly what I needed for the assignment! Thank you so much! :) |