2

I'm trying to send a basic email using Python 3.3. I'm following the first bit of code here:

https://docs.python.org/3.3/library/email-examples.html

My code is as follows:

def emailCurrentRankings(recipientEmail):
    fp = open('rankings.txt', 'rb')
    msg = MIMEText(fp.read())
    fp.close()

    sender = '[email protected]'
    msg['Subject'] = 'CSA Rankings'
    msg['From'] = sender
    msg['To'] = recipientEmail

    s = smtplib.SMTP('localhost')
    s.sendmail(sender, [recipientEmail], msg.as_string())
    s.quit()

My main function calls this method like so:

emailCurrentRankings('[email protected]')

The only difference I can tell is that I use 'rankings.txt' instead of textfile on the second line. I've tried with both and get the same error message:

Traceback (most recent call last):
  File "helpfulFunctions.py", line 128, in <module>
    main()
  File "helpfulFunctions.py", line 120, in main
    emailCurrentRankings('[email protected]')
  File "helpfulFunctions.py", line 106, in emailCurrentRankings
    msg = MIMEText(fp.read())
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/email/mime/text.py", line 34, in __init__
    _text.encode('us-ascii')
AttributeError: 'bytes' object has no attribute 'encode'

When I googled around, it looks like there's some authentication that has to happen (for me to be able to send from a given email). But their most basic example that I'm modeling my code on doesn't mention this...

Any ideas where I'm going astray?

Thanks, bclayman

4
  • Unless you have your own SMTP server, you need to change it to an external one (here smtplib.SMTP('localhost')). This won't solve your main problem which is probably due to an encoding error of the file. Try to follow this link to see how sending emails with attachments. Commented Jun 13, 2015 at 17:40
  • This is not an answer but I made a program to send emails a while ago. If you want you can check it out at github.com/paulkr/PyMail for reference. Commented Jun 13, 2015 at 17:43
  • @boh What should I be changing the server to? I don't have my own and not sure what external server I'd want to use... Commented Jun 13, 2015 at 19:03
  • 1
    Using gmail, I use the following one: smtp.gmail.com:587. Check the accept answer of a question of mine to allow your app sending emails through gmail: question link Commented Jun 14, 2015 at 11:28

1 Answer 1

3

Try openning the file without without using the binary format.

Maybe something like -

fp = open('rankings.txt', 'r')
Sign up to request clarification or add additional context in comments.

2 Comments

That helps one of my issues but not what boh references above. I still have an issue with "s = smtplib.SMTP('localhost')
For smtplib.SMTP("localhost") to work, you need to have a SMTP server installed on your localhost, which may not be the case. In such cases, you can use other online smtp servers, such as gmail or so. As given, in the comment in question - "Using gmail, I use the following one: smtp.gmail.com:587. Check the accept answer of a question of mine to allow your app sending emails through gmail: question link – boh"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.