I am writing a program to send an email (from message.txt) to email addresses which are stored in email.txt. Sending an empty email works flawlessly, but if I use the txt or the message it throws the syntax error "unexpected unindent" on the very last line of code. Why is that and what can I do to solve it?
EDIT: I have read through other people's problems getting the same error, but their error's origin is very different to mine, hence I cannot conclude anything from their solutions...
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(user, pwd, recipients, subject):
try:
with open('message.txt') as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
recipients = []
with open('emails.txt') as f:
for line in f:
if len(line.strip()) > 0:
recipients.append(line.strip())
#container
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your reminder'
msg['From'] = 'example@gmail.com'
msg['To'] = ','.join(recipients)
msg.attach(msg)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, pwd)
server.sendmail(user, recipients, msg.as_string())
server.close()
print("Sent the email!")
send_email("example@gmail.com", "password", "Subject", "A reminder")
You've used the variable name msg as both the MIMEText message attachment and the MIMEMultipart message container. In the code below, I've changed the name for the MIMEText variable to be strMessage.
The unexpected unindent occurs because the try block isn't complete. Either add something to the try block to complete the compound statement:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(user, pwd, recipients, subject):
try:
with open('message.txt') as fp:
# Create a text/plain message
strMessage = MIMEText(fp.read())
recipients = []
with open('emails.txt') as f:
for line in f:
if len(line.strip()) > 0:
recipients.append(line.strip())
#container
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your reminder'
msg['From'] = 'example@gmail.com'
msg['To'] = ','.join(recipients)
msg.attach(strMessage)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, pwd)
server.sendmail(user, recipients, strMessage.as_string())
server.close()
print("Sent the email!")
finally:
pass
send_email("example@gmail.com", "password", "Subject", "A reminder")
Or omit the try entirely:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(user, pwd, recipients, subject):
with open('message.txt') as fp:
# Create a text/plain message
strMessage = MIMEText(fp.read())
recipients = []
with open('emails.txt') as f:
for line in f:
if len(line.strip()) > 0:
recipients.append(line.strip())
#container
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your reminder'
msg['From'] = 'example@gmail.com'
msg['To'] = ','.join(recipients)
msg.attach(strMessage)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, pwd)
server.sendmail(user, recipients, strMessage.as_string())
server.close()
print("Sent the email!")
send_email("example@gmail.com", "password", "Subject", "A reminder")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.