I am trying to send an email using Python, when I pass the last field as variable it does not send anything in the email -
# web scraping import urllib import smtplib urllib.request import urlopen bs4 import beautifulsoup def make_soup(url): thepage= urllib.request.urlopen(url) soupdata = beautifulsoup(thepage, "html.parser") return soupdata soup = make_soup("http://www.met.gov.kw/?lang=eng") record in soup.select('#newforecast'): information = record.text content = information[48:440] msg = content
here part of code having problems when transfer information stored in content message , pass sendmail() function body message supposed go email shows empty can tell me going wrong?
server = smtplib.smtp('smtp.gmail.com', 587) server.starttls() server.login("youremail", "yourpassword") server.sendmail("placeholder", "placeholder", msg) server.quit()
your variable msg
raw string of content. content of email must use mime type (multipurpose internet mail extensions).
in python can use objects mimetext
(for text) or mimemultipart
(with attachment). convert content equivalent mime format function as_string()
. or can build own string mime format. ;)
here correction code. should work:
# import object mimetext email.mime.text import mimetext ... # build instance of mimetext content string msg = mimetext(content) # subject, from, information, receiver see # it's no problem, if use fake address here. that's way, how phishing mail or spam mail works msg['subject'] = 'subject of email' msg['from'] = "placeholder" msg['to'] = "placeholder" ... # use as_string() convert data equivalent mime format # can use `print msg.as_string()` see how is. server.sendmail("placeholder", "placeholder", msg.as_string())
more examples email examples
Comments
Post a Comment