0

I'm creating this project with my daughter to use with a smart bird feeder we are building together and this segment is holding us back and we just can't figure out what we're doing wrong. We are doing this all on a raspberry pi using vi as our editor. So, I am reaching out to the wider python3 community to see if we could get help and and learn something at the same time.
Thank you in advance for any help you could render.

Here is the code I've created so far:

import email, smtplib, ssl
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 465 for SSL or 587 for starttls
ssl_port = 465  
starttls_port = 587

smtp_server = "smtp.gmail.com"

# Pull these two in from another module.
sender_email = "*****@gmail.com"
sender_password = "********"
reciever_email = "*****@gmail.com"

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = reciever_email

# For text email message.
text = """\Hi, did this work? Just wanted to test this emailer out, thanks!"""

# For html email message.
html = """\
<html>
    <body>
        <p>Hi,<br>
           did this work?<br>
           Just wanted to test this emailer out, thanks!<br>
           <a href="https://encrypted-tbn2.gstatic.com/shopping?q=tbn:ANd9GcRLUWcAUNHlw2vdmRPyLwxvTccJ-imu7r0YAN1Tx-XBMu30yyr1-YowVJBRsob_9AQDWhg8cCq9UokYcJnJaxVHNGs___c6Z0GLgfN7ItsjpvlLfSc5C_plHA&usqp=CAc</a><br>
           A Golden Finch Visited us Today!
        </p>
    </body>
</html>
"""

# Turn these into plain/html MIMEText objects.
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")

# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first.
message.attach(part1)
message.attach(part2)

# Create a secure SSL context and email
context = ssl.create_default_context()

with smtplib.SMTP_SSL(smtp_server, ssl_port, context=context) as server:
    server.login(sender_email, sender_password)
    server.sendmail(sender_email, reciever_email, message.as_string())

# Try to log into server and send email.
try:
    server = smtplib.SMTP()
    server.connect(smtp_server, starttls_port)
    server.starttls(context=context) # Secure the connection
    server.login(sender_email, sender_password)
    server.sendmail(sender_email, reciever_email, message)
except Exception as e:
    print(e)
finally:
    server.quit()

I keep getting the the following error:

File ".\emailer_test.py", line 58
    server.connect(smtp_server, starttls_port)
                                             ^
IndentationError: unindent does not match any outer indentation level
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    I copied the snippet and it passed that line without a problem. Are you using white spaces or tabs to indent? Mixing the two can cause trouble. Although that would result in a different error message. – Peter Devenyi Jul 07 '21 at 01:24
  • @Peter You got it! If you look at [the source](https://stackoverflow.com/revisions/9d1d3914-8a54-4770-b26c-6f27b7d0af27/view-source), it's mixed tabs and spaces. – wjandrea Jul 07 '21 at 04:45
  • Does this answer your question? [IndentationError with Python](https://stackoverflow.com/questions/47052039/indentationerror-with-python) – wjandrea Jul 07 '21 at 04:48
  • Thank you, that was it! :) " – Joseph McTigue Jul 18 '21 at 19:08

0 Answers0