Python Forum
Simple e-mail sender, that automatically changes subject for every e-mail
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple e-mail sender, that automatically changes subject for every e-mail
#1
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. Sad May be someone can pinpoint the right approach? Smile

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! :)
Reply
#2
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)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
This is exactly what I needed for the assignment! Thank you so much! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get sender of email Pedroski55 0 1,507 Dec-03-2022, 09:23 AM
Last Post: Pedroski55
  color column in mail html df bnadir55 0 687 Aug-14-2022, 07:11 AM
Last Post: bnadir55
  get sender IP on UDP server? korenron 2 2,616 Jun-21-2021, 03:11 PM
Last Post: korenron
  Mail issue Mihil 3 2,588 Dec-03-2020, 05:25 AM
Last Post: Mihil
  Mail Merge from DataFrame into Word Label Template PEGylated_User 0 1,913 Nov-10-2020, 01:01 PM
Last Post: PEGylated_User
  A mail scraper for sorting emails into categories anata2047 1 5,403 Sep-03-2019, 11:03 PM
Last Post: micseydel
  o365 special subject mail download issue anna 3 2,849 May-16-2019, 07:16 PM
Last Post: micseydel
  smtplib mail without subject anna 2 2,431 Apr-24-2019, 05:44 AM
Last Post: anna
  Mail sending Python - What it's wrong? airwawekz 1 2,662 Jan-30-2019, 03:20 PM
Last Post: gontajones
  How can read and download the attachment from the mail using while loop in IMAPlib Py Samjith 0 4,219 Oct-11-2018, 07:15 AM
Last Post: Samjith

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020