Search code examples
pythonsqlitesyntaxmime

SyntaxError in MIMEText and sqlite


This code send a notification about new article which is stored in database specified earlier in my program (this is element of RSS feeder).

def send_notification(article_title, article_url):
    smtp_server=smtplib.SMTP('smtp.gmail.com', 587)
    smtp_server.ehlo()
    smtp_server.starttls()
    smtp_server.login('your_email@gmail.com','password')
    msg = MIMEText(f'\nHi, this is new article: {article_title}. \nYou can read this in {article_url}')
msg['Subject']='New article is available'
msg['From']='your_email@gmail.com'
msg['To']='destination_email@gmail.com'
smtp_server.send_message(msg)
smtp_server.quit()

I receive a message "SyntaxError: invalid syntax" for line:

msg = MIMEText(f'\nHi, this is new article: {article_title}. \nYou can read this in {article_url}')

I think it is caused by {} parentheses. Could anyone help me to fix it?

PS I work in Python3.


Solution

  • This should solve your syntax problem:

    msg = MIMEText('\nHi, this is new article: {}. \nYou can read this in {}'.format(article_title, article_url))
    

    Read more about string formatting in python here