0

I created a function that prints the contents of a web page based on certain keywords and also based on the date. What i wanted is to receive that output by email, but i just can't seem to find a way to do it. I also haven't found an answer for this. I'm sorry if it already exists, but i did spend a few hours looking for it and found nothing. There doesn't seem to be that much on this topic other than "how to send a mail".

def content():
for d3 in d1:
    if (x or y or k or w or f) and date in d3.text.upper().lower():
        print(d3.text)
        print(d3.attrs['href'])

As i said, this works just fine and prints what i wanna see. The problem is that i wanna receive it by mail. I've also created mail function to send email:

def send_mail():
sender_mail="*****@gmail.com"
receiver_mail="*****@gmail.com"
password="*******"
message="hey"
server= smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_mail,password)
print("login success")
server.sendmail(sender_mail, receiver_mail, message)
print("email sent")

As it is, this also works perfectly fine. It does send me an email. But i don't want a text message, i want the output of my function. I've tried to put the previous function in there, but it doesn't work.

Is there a way to do this?

Bruno K
  • 1
  • 2
  • Welcome to SO. Please take the [tour](https://stackoverflow.com/tour), read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Please fix the indentation in your code. Python is very sensitive to indentation. – Michael Ruth Sep 04 '21 at 18:35
  • Your indentation is crearly wrong, but it's less obvious exactly what it should be. Please [edit] your question to include actually syntactically valid Python code. On the desktop version of this site, paste your code, then select the pasted block and type ctrl-K to correctly format your code with indentations preserved. – tripleee Sep 04 '21 at 20:04

1 Answers1

0

The problem isn't about sending mail, it's about getting a value out of a function, in this case: content().

The function needs to return a value rather than printing values to stdout:

def content():
    buf = ""
    for d3 in d1:
        if (x or y or k or w or f) and date in d3.text.upper().lower():
            buf += d3.text + "\n" + d3.attrs["href"] + "\n"
    return buf


def send_mail():
    sender_mail="*****@gmail.com"
    receiver_mail="*****@gmail.com"
    password="*******"
    message=content()
    server= smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(sender_mail,password)
    print("login success")
    server.sendmail(sender_mail, receiver_mail, message)
    print("email sent")

Consider changing buf from a str to an io.StringIO if it's expected to be large. Also consider refactoring your code so that it doesn't rely on global-ness of values like x, y, k, w, f, etc.

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • Thanks for your time. Unfortunely it still doesn't do what i need. I get this erros: line 859, in sendmail msg = _fix_eols(msg).encode('ascii') and then: 'ascii' codec can't encode character '\xe1' in position 39: ordinal not in range(128) – Bruno K Sep 04 '21 at 18:54
  • @BrunoK, that is a different problem. Fortunately, it has been [solved](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) many times on SO. – Michael Ruth Sep 04 '21 at 19:00
  • Thank you. Managed to solve the problem by adding .encode('utf-8').strip() to the "buf" variable. Email was sent exactly as i wanted. – Bruno K Sep 05 '21 at 00:22