Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:36:58+00:00 2026-06-16T22:36:58+00:00

I have written a script that writes a message to a text file and

  • 0

I have written a script that writes a message to a text file and also sends it as an email.
Everything goes well, except the email finally appears to be all in one line.

I add line breaks by \n and it works for the text file but not for the email.
Do you know what could be the possible reason?


Here’s my code:

import smtplib, sys
import traceback
def send_error(sender, recipient, headers, body):

    SMTP_SERVER = 'smtp.gmail.com'
    SMTP_PORT = 587
    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.ehlo()
    session.starttls()
    session.ehlo
    session.login(sender, 'my password')
    send_it = session.sendmail(sender, recipient, headers + "\r\n\r\n" +  body)
    session.quit()
    return send_it


SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'sender_id@gmail.com'
recipient = 'recipient_id@yahoo.com'
subject = 'report'
body = "Dear Student, \n Please send your report\n Thank you for your attention"
open('student.txt', 'w').write(body) 

headers = ["From: " + sender,
               "Subject: " + subject,
               "To: " + recipient,
               "MIME-Version: 1.0",
               "Content-Type: text/html"]
headers = "\r\n".join(headers)
send_error(sender, recipient, headers, body)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:36:59+00:00Added an answer on June 16, 2026 at 10:36 pm

    You have your message body declared to have HTML content ("Content-Type: text/html"). The HTML code for line break is <br>. You should either change your content type to text/plain or use the HTML markup for line breaks instead of plain \n as the latter gets ignored when rendering a HTML document.


    As a side note, also have a look at the email package. There are some classes that can simplify the definition of E-Mail messages for you (with examples).

    For example you could try (untested):

    import smtplib
    from email.mime.text import MIMEText
    
    # define content
    recipients = ["recipient_id@yahoo.com"]
    sender = "sender_id@gmail.com"
    subject = "report reminder"
    body = """
    Dear Student,
    Please send your report
    Thank you for your attention
    """
    
    # make up message
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ", ".join(recipients)
    
    # sending
    session = smtplib.SMTP('smtp.gmail.com', 587)
    session.starttls()
    session.login(sender, 'my password')
    send_it = session.sendmail(sender, recipients, msg.as_string())
    session.quit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that writes a text file to the same directory
The script I have written outputs all lines from the file 2 that starts
I have written a script that on execution lets the user record a message
How do I delay a PHP script that writes to a text file and
If I have a script that writes 1000 lines to file and then continues
I have written a bash script that writes my computer's current dynamic ip address
I have written a udev rule that call a script to send email when
I have written a script that goes through a bunch of files and snips
I have written a script that fires a jQuery POST to retrieve data from
So I have a strange question. I have written a script that re-formats data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.