Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.I have a function to send an email to someone ,but I want to insert a new line wherever I want (in the message I send)

What I have tried:

Python
def email_send(SUBJECT , message):
    print("send email...")
    FROM = "my email"
    TO = "his/her email"

    MESSAGE = MIMEMultipart('alternative')
    MESSAGE['subject'] = SUBJECT
    MESSAGE['To'] = TO
    MESSAGE['From'] = FROM
    HTML_BODY = MIMEText(message, 'html')
    MESSAGE.attach(HTML_BODY)
    server = smtplib.SMTP("smtp.gmail.com:587")    
    password = "my code"
    server.starttls()
    server.login(FROM,password)
    server.sendmail(FROM , TO , MESSAGE.as_string() )
    server.quit()
    return TO



I run the code like this:
Python
email_send("Some title" , "Hello\nHow\nare\nyou")



I want to send:
Hello
How
are
you


but instead I send "Hello how are you"
I also tried to put '\r\n' and '\r\r\n' but nothing of these work
What should I do?
Thanks in advance.
Posted
Comments
Richard MacCutchan 7-Feb-22 5:11am    
You probably need to tell the emailer that your message is plain text.
Nick_is_asking 7-Feb-22 5:13am    
Never mind.I found the solution.
I had to write "" instead of "\n".
But thank you for your time.

1 solution

please check the version of python.
for version 3.11.1,
you should use '\r\n' for line break
reference the official doc smtplib — SMTP protocol client — Python 3.11.1 documentation[^]
 
Share this answer
 
Comments
Dave Kreskowiak 12-Jan-23 23:25pm    
It depends on the type of content in the email body. In the OP's case, from a year ago, it's HTML, which does not recognize \r\n as a line break.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900