How can I send variants by email using python? The email is sent but not with the variants. Here is the code:
\`import email.message import os import smtplib import sys
def restart_program(): python = sys.executable os.execl(python, python, \* sys.argv)
print('Enter the first element:') first_element = (input()) print('Enter the second element:') second_element = (input()) print('Enter the result:') result = (input())
def send_email(): email_body = """ \[ { production1: first_element production2: second_element result: result #I want that the variants: "first_element" "second_element" and "result" be send in the email as the user writes in the input } \] """ from_addr = 'CENSURED' to_addr = 'CENSURED' password = 'CENSURED'
msg = email.message.Message() msg.set_payload(email_body)
server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(from_addr, password) server.sendmail(from_addr, to_addr, msg.as_string()) server.quit()
print("It's correct?") print(first_element) print("+") print(second_element) print("=") print(result) print('Confirm') Confirm = (input()) if Confirm.lower() == "yes": print("Sending Email...") Send_email() print('Email sent!') restart_program() else: print("Try Again") restart_program()\`\`
I tried putting quotation marks in the variants and using strings:
def send_email(): email_body = """ [ { production1: %s %first_element production2: %s %second_element result: %s %result } ] """
(I'm a newbie in programation so I probably used it wrong)
but it dosent work, I want that the words the user writes in the input be writen in the email body insted of the name of the variants, for instance: if the user writes test in the input of the first_element, test(1) in the second_element and test(2) in the result, in the body be like this:
[ { production1: test production2: test(1) result: test(2) } ]
If you want to include variables in a string message, you have several options.
The easiest way is to format your message using F-strings:
message = f"Hello {name}, I am glad to meet you."
Because there is an f
before the quote mark at the start of the string, any expressions enclosed in curly braces like {name}
or {x+y}
will be replaced with the string representation of the value of the expression.