Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in smtplib example #120662

Closed
MordechaiFast opened this issue Jun 17, 2024 · 5 comments
Closed

Bug in smtplib example #120662

MordechaiFast opened this issue Jun 17, 2024 · 5 comments
Labels
docs Documentation in the Doc dir

Comments

@MordechaiFast
Copy link

MordechaiFast commented Jun 17, 2024

Documentation

The example code is meant to demonstrate sending a multi-line message. It does not do that; the lines get contracted into one.

This can be fixed by adding a newline character to the end of the line when it is added to the message string:

msg += line + '\n'

BTW, why not use PEP8 compliant names, f-strings, and context managers? Why make things complicated with a .strip() call? Why make it that blank lines in the message are like pressing send?

import smtplib


from_addr = input("From: ")
to_addrs  = input("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")

# Add the From: and To: headers at the start!
msg = f"From: {from_addr}\r\nTo: {', '.join(to_addrs)}\r\n\r\n"
while True:
    try:
        line = input()
    except EOFError:
        break
    msg += line + '\n'

print("Message length is", len(msg))

with smtplib.SMTP('localhost') as server:
    server.set_debuglevel(1)
    server.sendmail(from_addr, to_addrs, msg)

Linked PRs

@MordechaiFast MordechaiFast added the docs Documentation in the Doc dir label Jun 17, 2024
@hugovk hugovk changed the title Bug in stmplib example Bug in smtplib example Jun 18, 2024
@hugovk
Copy link
Member

hugovk commented Jun 18, 2024

Please can you link to the relevant page?

@terryjreedy
Copy link
Member

The proper idiom would be to make an initialized list of lines, append further lines, and join with '\r\n' when complete.

    lines = [f"From: {from_addr}", "To: {', '.join(to_addrs)}", ""]
...
        lines.append(line)
...
    msg = '\r\n'.join(lines)

@picnixz
Copy link
Contributor

picnixz commented Jun 18, 2024

BTW, why not use PEP8 compliant names, f-strings, and context managers?

Well... PEP 8 is a suggestion, not a requirement so it can't force people to follow it. For the rest, it's probably because at the time when it was written, f-strings and context managers did not exist and the code has not been updated since then.

Why make things complicated with a .strip() call?

To ensure that you don't have whitespaces before and after in the list of mails to send to, just before the split() call (and to avoid having an empty address).

Why make it that blank lines in the message are like pressing send?

that's a good question; I think it was assumed that you only want to send a single paragraph (though I agree it's not really a good idea since you could just end the message with CTRL+D).


I'll make a PR to improve the example. @terryjreedy May I shamefully take your idiom?

By the way Hugo, the page is https://docs.python.org/3/library/smtplib.html#smtp-example.

vstinner pushed a commit that referenced this issue Jun 18, 2024
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 18, 2024
(cherry picked from commit 4bc27ab)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 18, 2024
(cherry picked from commit 4bc27ab)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
vstinner pushed a commit that referenced this issue Jun 18, 2024
gh-120662: Improve `smtplib` example (GH-120668)
(cherry picked from commit 4bc27ab)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
vstinner pushed a commit that referenced this issue Jun 18, 2024
gh-120662: Improve `smtplib` example (GH-120668)
(cherry picked from commit 4bc27ab)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@picnixz
Copy link
Contributor

picnixz commented Jun 18, 2024

I think this one can now be closed @hugovk

@terryjreedy
Copy link
Member

@picnixz (" May I shamefully take your idiom?") Yes! I am delighted to wake up and find my suggestion implemented and merged.

picnixz added a commit to picnixz/cpython that referenced this issue Jun 19, 2024
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

5 participants