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.
Your code is very messy, very distorted, but if you copy your line 18 to line 25 it will work.
Anyway I recommend some clean up.
Cheers and Happy coding
Your code is very messy, very distorted, but if you copy your line 18 to line 25 it will work.
Anyway I recommend some clean up.
Cheers and Happy coding
Thanks for the reply. As for the code, I will give it a make-over once I solve this one. When I did as you told, I started to get empty mails, no subject or text whatsoever.
And like this?
import nltk
import os
import smtplib
import time
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import encoders
from nltk.corpus import gutenberg
from random import choice
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-
class gmail():
def __init__(self):
self.recipient = "xxxxxxx@hotmail.com"
self.gmail_user = "asdassdsaadssd"
self.gmail_pwd = "asdasdasasdasasd"
self.subject = "assadasdasasdass"
def create_mail(self, message):
self.themail = MIMEMultipart()
self.themail['Subject'] = self.subject
self.themail['From'] = self.gmail_user
self.themail['To'] = self.recipient
self.themail.attach(MIMEText(message))
def send_mail():
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.login(self.gmail_user, self.gmail_pwd)
print "Connection established."
smtpserver.sendmail(self.gmail_user, self.recipient, self.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
mail = gmail()
mail.create_mail(message)
mail.send_mail()
print "\n"
lastLongestStr = longestString
time.sleep(5)
Cheers and Happy coding
All variables in create_mail are local to create_mail and can not be used in send_mail. One solution is to use one function only, which simplifies things. See here for a starter on how to use funtions.
def create_mail(message):
themail = MIMEMultipart()
recipient = "xxxxxxx@hotmail.com"
gmail_user = "asdassdsaadssd"
gmail_pwd = "asdasdasasdasasd"
subject = "assadasdasasdass"
themail['Subject']=subject
themail['From']=gmail_user
themail['To']=recipient
themail.attach(MIMEText(message))
## comment out so everything is now part of create_mail()
##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()All variables in create_mail are local to create_mail and can not be used in send_mail. One solution is to use one function only, which simplifies things.
def create_mail(message): themail = MIMEMultipart() recipient = "xxxxxxx@hotmail.com" gmail_user = "asdassdsaadssd" gmail_pwd = "asdasdasasdasasd" subject = "assadasdasasdass" themail['Subject']=subject themail['From']=gmail_user themail['To']=recipient themail.attach(MIMEText(message)) ## comment out so everything is now part of create_mail() ##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()
That hit the spot, thanks a ton.