Posts: 12
Threads: 5
Joined: Feb 2021
This python code writing configuration file for getmail to retrieve the mail, in the first and last f.write code i use the "\n" to break the lines for new line:
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}', 'w') as f:
f.write('[options]\n verbose = 1\n read_all = false\n delete = true\n received = false\n delivered_to = false\n message_log = /home/mail/log\n message_log_verbose = true\n ')
f.write('username = ')
f.write(row[0])
f.write("\n")
f.write(' password = ')
f.write(row[1])
f.write("\n")
f.write(' server = ')
f.write(row[2])
f.write("\n")
f.write('[destination]\n type = MDA_external\n path = /usr/bin/exim\n arguments = ("-i", "-bm", "user@localhost")\n unixfrom = true\n ') with cat the file seem ok there is no '\n' in the end of lines, but when run the getmail get the error:
Configuration error: configuration file /home/mail/mail1 incorrect (File contains parsing errors: /home/mail/mail1
[line 2]: ' verbose = 1\n'
[line 3]: ' read_all = false\n'
[line 4]: ' delete = true\n'
[line 5]: ' received = false\n'
[line 6]: ' delivered_to = false\n' how to remove the '\n' from the end of lines.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Feb-03-2021, 04:43 PM
(This post was last modified: Feb-03-2021, 04:43 PM by bowlofred.)
If you remove the newline, they won't be separate lines.
I suspect the error is telling you that you have whitespace at the beginning of the lines. For getmail, the configuration assumes that if it starts with whitespace it is a continuation of the previous line.
Quote:A parameter value, if necessary, can span multiple lines. To indicate that the second and subsequent lines form a continuation of the previous line, they need to begin with leading whitespace, like this:
first_parameter = value
first parameter value continues here
second_parameter = value
Get rid of the leading whitespace.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Feb-03-2021, 05:53 PM
(This post was last modified: Feb-03-2021, 05:53 PM by buran.)
instead of writing the file like you do, look at configparser module from standard library. Your code will be way more clean and readable
import configparser
my_config="""[options]
verbose = 1
read_all = false
delete = true
received = false
delivered_to = false
message_log = /home/mail/log
message_log_verbose = true
[destination]
type = MDA_external
path = /usr/bin/exim
arguments = ("-i", "-bm", "user@localhost")
unixfrom = true"""
config = configparser.ConfigParser()
config.read_string(my_config)
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
credentials = dict(zip(('username', 'password', 'server'), row))
config['options'].update(credentials)
with open(f'mail{idx}', 'w') as f:
config.write(f) instead of string my_config can be a dict
Also I don't know if MariaDB python connector supports DictCursor like MySQL connector, but if it does - you can use it.
bowlofred likes this post
Posts: 12
Threads: 5
Joined: Feb 2021
Feb-04-2021, 07:00 AM
(This post was last modified: Feb-04-2021, 07:02 AM by shams.)
Thanks for replies, yes also the problem was with the white spaces after \n i remove the all spaces after \n now it working, also special thanks to buran for the perfect code, it is working nice, the only thing is i forgot to add two lines (one section), i tried to add these lines to the code but failed, can you please add these two lines to the code so the configuration file just look like this:
[options]
verbose = 1
read_all = false
delete = true
received = false
delivered_to = false
message_log = /home/mail/log
message_log_verbose = true
[retriever]
type = SimplePOP3Retriever
username = [email protected]
password = ******
server = pop.gmail.com
[destination]
type = MDA_external
path = /usr/bin/exim
arguments = ("-i", "-bm", "user@localhost")
unixfrom = true
Posts: 8,160
Threads: 160
Joined: Sep 2016
(Feb-04-2021, 07:00 AM)shams Wrote: i tried to add these lines to the code but failed C'mon, it couldn't be that difficult. Show what have you tried
Posts: 12
Threads: 5
Joined: Feb 2021
i added the lines as this:
my_config="""[options]
verbose = 1
read_all = false
delete = true
received = false
delivered_to = false
message_log = /home/mail/log
message_log_verbose = true
[retriever]
type = SimplePOP3Retriever
[destination]
type = MDA_external
path = /usr/bin/exim
arguments = ("-i", "-bm", "user@localhost")
unixfrom = true"""
config = configparser.ConfigParser()
config.read_string(my_config)
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
credentials = dict(zip(('username', 'password', 'server'), row))
config['options'].update(credentials)
with open(f'mail{idx}', 'w') as f:
config.write(f) and this was the output:
[options]
verbose = 1
read_all = false
delete = true
received = false
delivered_to = false
message_log = /home/mail/log
message_log_verbose = true
username = [email protected]
password = ******
server = pop.gmail.com
[retriever]
type = SimplePOP3Retriever
[destination]
type = MDA_external
path = /usr/bin/exim
arguments = ("-i", "-bm", "user@localhost")
unixfrom = true i changed the double quotes to the deferent positions but no luck.
Posts: 8,160
Threads: 160
Joined: Sep 2016
look at line 27. You need to update that too, to update retriever section, not options .
Posts: 12
Threads: 5
Joined: Feb 2021
Thanks for the help i just changed to update the retriever instead of option, now every thing is going ok.
|