Simple Python script as SMTP server for outgoing e-mails?

D

Dennis Lee Bieber

I don't think it is. Behaviour is a little different between Python 2 and
3, but by default, Python uses "Universal Newlines". When you open a file

Oh? When did that change come about... So far as I recall, Python 2.x
introduced a "universal newline" mode character for the open() function.
IE, one had to explicitly ask for universal mode during the open.

Python 2.7 help file
"""
open(filename[, mode[, bufsize]])

<snip>

to 'r'. The default is to use text mode, which may convert '\n' characters
to a platform-specific representation on writing and back on reading. Thus,
when opening a binary file, you should append 'b' to the mode value to open
the file in binary mode, which will improve portability. (Appending 'b' is

<snip>

In addition to the standard fopen() values mode may be 'U' or 'rU'. Python
is usually built with universal newline support; supplying 'U' opens the
file as a text file, but lines may be terminated by any of the following:
the Unix end-of-line convention '\n', the Macintosh convention '\r', or the
Windows convention '\r\n'. All of these external representations are seen
as '\n' by the Python program. If Python is built without universal newline
support a mode with 'U' is the same as normal text mode. Note that file
objects so opened also have an attribute called newlines which has a value
of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple
containing all the newline types seen.
"""

Seems to be silent with regards to /writing/; suspect writing will use
the system convention unless using "wb"/"ab" to force binary mode.
 
D

Dennis Lee Bieber

Correctness is a worthwhile reason to do something :)

I've been reading a collection of "Liaden" short stories... and for
some reason that phrasing sounds a lot like the more formal Liaden speech
patterns. {Liaden culture seems heavy on personal honor, and comments tend
(to me) be worded to avoid any chance of being interpreted as disparaging
of the person with whom one is speaking... Hmmm, pity such modes can't be
enforced on the newsgroups <G>}
 
S

Steven D'Aprano

{Liaden culture seems heavy on personal honor, and comments tend (to me)
be worded to avoid any chance of being interpreted as disparaging of the
person with whom one is speaking... Hmmm, pity such modes can't be
enforced on the newsgroups <G>}

Are you implying that failure to avoid disparaging others in newsgroups
is harmful? That disparages me.
 
V

Vincent Vande Vyvre

Le 23/07/2013 17:25, Steven D'Aprano a écrit :
On Windows a script where de endline are the system line sep, the files
are open with a double line in Eric4, Notepad++ or Gedit but they are
correctly displayed in the MS Bloc-Notes.
I suspect the problem lies with Eric4, Notepad++ and Gedit. Do you
perhaps have to manually tell them that the file uses Windows line
separators?

I recommend opening the file in a hex editor and seeing for yourself what
line separators are used.

Example with this code:
----------------------------------------------
# -*- coding: utf-8 -*-

import os
L_SEP = os.linesep

def write():
strings = ['# -*- coding: utf-8 -*-\n',
'import os\n',
'import sys\n']
with open('writetest.py', 'w') as outf:
for s in strings:
outf.write(s.replace('\n', L_SEP))

write()
I don't think it is. Behaviour is a little different between Python 2 and
3, but by default, Python uses "Universal Newlines". When you open a file
in text mode, arbitrary line separators should be automatically
translated to \n when reading, and \n will be automatically translated to
os.line_sep when writing.


http://docs.python.org/3/library/functions.html#open
http://docs.python.org/2/library/functions.html#open

Some further discussion here:

http://stackoverflow.com/questions/12193047/is-universal-newlines-mode-
supposed-to-be-default-behaviour-for-open-in-python
In fact, in my code, the original file is open in binary mode, the line
separator is translate to \n and it is parsed by the module tokenise.

I'm not a Windows user but my code must be run also on Win, this is the
reason of the usage of os.linesep in writting.

So, now I found the solution, just write the file in binary mode and now
it is correctly open with all the editors.

Thanks all
 
C

Chris Angelico

In fact, in my code, the original file is open in binary mode, the line
separator is translate to \n and it is parsed by the module tokenise.

I'm not a Windows user but my code must be run also on Win, this is the
reason of the usage of os.linesep in writting.

So, now I found the solution, just write the file in binary mode and now it
is correctly open with all the editors.

Sounds to me like the problem was double-translation - you in your
code turned \n into \r\n, but then the file was written in text mode,
doing the same translation, so your lines were terminated with \r\r\n.
That would result in what you're seeing (including the oddity that
some editors will show the file differently).

You may find it easier to write the file in text mode and let the
underlying system do the translation for you. Chances are that'll be
correct.

ChrisA
 
K

Kevin Walzer

Thanks. hMailServer was one of the apps I checked, and I was just
making sure there weren't something simpler, considering my needs,
ideally something like Mongoose MTA.

Regardless, because of the SPAM anti-measures mentioned above, it
seems like I was over-optimistic about running an MTA and sending
e-mails from my home computer :-/

The reason I mentioned hMailServer is that I host my own mail server for
my business--I have a static IP address, and correctly configured
DNS--and so I'm able to send out large batches of e-mails to customers
from my network without being blocked by my ISP. I'm running a Mac
server so my mail system is the typical Unix setup (Postfix, etc.), but
hMailServer was the closest thing I've found for Windows.

Configuring your own server isn't cheap in terms of time even if you use
FOSS components, and your ISP may charge more for a static IP, so I
completely understand if you don't want to go that route.

--Kevin
 
T

Terry Reedy

I suspect the problem likes in the file written. Notepad++ works fine
with \r\n or \n on input and can produce either on output.
 
G

Gilles

The reason I mentioned hMailServer is that I host my own mail server for
my business--I have a static IP address, and correctly configured
DNS--and so I'm able to send out large batches of e-mails to customers
from my network without being blocked by my ISP. I'm running a Mac
server so my mail system is the typical Unix setup (Postfix, etc.), but
hMailServer was the closest thing I've found for Windows.

Configuring your own server isn't cheap in terms of time even if you use
FOSS components, and your ISP may charge more for a static IP, so I
completely understand if you don't want to go that route.

Thanks for the infos. Indeed, it seems like hMailServer is one of the
few good MTAs for Windows.

I already have a static IP, so the issue is more that remote MTAs
might not accept connections from MTAs running on users' PC instead of
ISP's.
 
K

Kevin Walzer

I already have a static IP, so the issue is more that remote MTAs
might not accept connections from MTAs running on users' PC instead of
ISP's.

For what it's worth, that hasn't been my experience.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top