Hello everyone, I have created a text generator that acts on certain factors. I want to send every generated sentence via e-mail to a user but I noticed that everytime I send it, I encounter some problems. Below I show my code and under my code, I will describe the error along with examples.
Here is my code:
import nltk
from nltk.corpus import gutenberg
from random import choice
import time
triggerSentence = raw_input("Please enter the trigger sentence: ")#get input str
lastLongestStr = ""
listOfSents = gutenberg.sents('austen-emma.txt') #all sentences of gutenberg are assigned -list of list format-
listOfWords = gutenberg.words('austen-emma.txt')# all words in gutenberg books -list format-
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import encoders
themail = MIMEMultipart()
recipient = "xxxxxxx@hotmail.com"
gmail_user = "asdassdsaadssd"
gmail_pwd = "asdasdasasdasasd"
subject = "assadasdasasdass"
def create_mail():
themail['Subject']=subject
themail['From']=gmail_user
themail['To']=recipient
themail.attach(MIMEText(message))
def send_mail():
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
print "Connection established."
smtpserver.sendmail(gmail_user, recipient, themail.as_string())
print "Your mail has been sent."
smtpserver.close()
while triggerSentence:
longestLength = 0
longestString = ""
longestLen2 = 0
longestStr2 = ""
#run the loop so long as there is a trigger sentence
sets = []
split_str = triggerSentence.split()#split the sentence into words
#code to find the longest word in the trigger sentence input
for piece in split_str:
if len(piece) > longestLength:
longestString = piece
longestLength = len(piece)
if longestString == lastLongestStr:
longestString = sorted(split_str, key=len)[-2]
print longestString + " second longest!"
if longestString == lastLongestStr:
longestString = sorted(split_str, key=len)[-3]
print longestString + " third longest!"
print longestString + " is now the longest"
print "\n"
#code to get the sentences containing the longest word, then selecting
#random one of these sentences that are longer than 40 characters
for sentence1 in listOfSents:
if sentence1.count(longestString):
sents1= " ".join(sentence1)
if len(sents1) > 40:
sets.append(sents1)
sents1 = ""
triggerSentence = choice(sets)
print triggerSentence
message = triggerSentence
create_mail()
send_mail()
message = ""
print "\n"
lastLongestStr = longestString
time.sleep(5)
Now here is the problem. Every time I send the mail , the subject of mail duplicates. It is like the following:
MySubject ->subject of 1st mail
MySubject MySubject -> subject of 2nd mail
Also I have this problem where the mail I send contains the contents of previous mail. Example:
2nd MAIL LAYOUT
1st mail contents (written in different font as if it is a reply or quotation from previous mail or something)
2nd mail contents
END OF MAIL
How do I solve this ? Thanks in advance.