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

  • Home
  • SEARCH
  • 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 8805735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:56:33+00:00 2026-06-14T01:56:33+00:00

I’m trying to send a text message using Gmail and the smtplib module in

  • 0

I’m trying to send a text message using Gmail and the smtplib module in Python.

The basic functionality is implemented, and using the following code, I’m perfectly able to receive these text messages on my phone:

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('email', 'password')
server.sendmail('email', 'recipient', '\nhttp://apple.com') 

Now, it might be of some utility to mention I am sending a link. One might wonder why I inserted the \n in front of the the example link; apparently, without it, smtplib processes the link as a header and it therefore sends an empty message.

However, a newline is added to my message, like so:

http://apple.com
newline here

I’m unable to understand why this happening:

  • is smtplib adding a newline because the message is a link ?
  • is it because I receive the message as an SMS?
  • is Gmail adding the newline ?
  • 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-14T01:56:34+00:00Added an answer on June 14, 2026 at 1:56 am

    The msg argument to sendmail is not the message body, but the entire message. So, it has to be a valid RFC(2)822 message.

    As the note at the end of the documentation says:

    In general, you will want to use the email package’s features to construct an email message, which you can then convert to a string and send via sendmail(); see email: Examples.

    However, the example directly above that shows how to generate a simple message yourself:

    # Add the From: and To: headers at the start!
    msg = ("From: %s\r\nTo: %s\r\n\r\n"
           % (fromaddr, ", ".join(toaddrs)))
    

    If you want to know what constitutes a valid email message, see RFC 2822 (and possibly RFC 822, which 2822 replaced, but which has some usefully different explanations and examples). But the short version is this: It’s a sequence of header lines, each ending in \r\n, followed by a blank line (meaning \r\n\r\n), followed by a message body.

    So, technically speaking, your code shouldn’t even work, because it just has a \n instead of a \r\n prefix.

    More importantly, you’re sending a message with no headers at all. Not even the From: and To: that you probably think you’re including. As the documentation for sendmail says:

    The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents. The SMTP does not modify the message headers in any way.

    This may or may not be considered invalid (or spam) by various servers.

    Meanwhile, the extra newline at the end is because all lines must end with a \r\n, and SMTP is a line-oriented protocol. There’s simply no way to signal that the message is done if it doesn’t end with a newline.

    If you want to understand the details, see RFC2821, which explains the SMTP protocol. In particular, look at 2.3.1 Mail Objects, 2.3.7 Lines, and 4.1.1.4 DATA (DATA). But the short version is that sending a message requires a DATA command, which must end with \r\n.\r\n, and "the first of this terminating sequence is also the that ends the final line of the data (message text)". This means there’s no way to send a message that doesn’t end with a newline.

    If you really want to, it’s possible to get around this by encapsulating your body in a MIME message; the MIME message itself will of course end with a newline, but that won’t affect your encapsulated body. There are a few ways to do this—but you’ll need the recipient to be able to handle whatever you send, and I’m guessing none of those ways will work with the email-SMS gateway (much less the SMS-receiving endpoint).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.