shawwwn/uMail

Error in parsing receivers address list

Closed this issue · 8 comments

Hello

in the to function, the parsing is not working

instead of having

    if isinstance(addrs, str):
        addrs = [addrs]
    count = 0
    for addr in addrs_to:

we must have something like

    if isinstance(addrs, str):
        addrs_to = addrs.split()  # addrs.split(",") 
    count = 0
    for addr in addrs_to:

of course the string of receivers must have the same separator

The idea was, when having multiple recipient addresses, supply the function with an array instead of a string, cause it's more pythonic this way.

smtp.to(['a@gmail.com', 'b@gmail.com'], mail_from='my@gmail.com')

You can read more about it here

...
C: MAIL FROM:<Smith@bar.com>
S: 250 OK
C: RCPT TO:<Jones@foo.com>
S: 250 OK
C: RCPT TO:<Green@foo.com>
S: 550 No such user here
C: RCPT TO:<Brown@foo.com>
S: 250 OK
...

yes, no problem, but converting a string to array by
addrs = [addrs]
cannot work

yes, no problem, but converting a string to array by
addrs = [addrs]
cannot work

I failed to understand your point, please elaborate.

try this code in regular CPython, see comments

# from original code

addrs = "alpha@foo.com,beta@foo.com,gamma@foo.com,delta@foo.com"
print(len(addrs), type(addrs))          # 54 <class 'str'>

if isinstance(addrs, str):
    addrs = [addrs]                     # not OK to change a str to a list
    print(len(addrs), type(addrs))      # 1 <class 'list'>

count = 0
for addr in addrs:
    print(addr)                         # alpha@foo.com,beta@foo.com,gamma@foo.com,delta@foo.com
    # code, resp = self.cmd('RCPT TO: <%s>' % addr)   # cannot work, incorrect SMTP syntax resulting
    # we have as result
    # code, resp = self.cmd('RCPT TO: alpha@foo.com,beta@foo.com,gamma@foo.com,delta@foo.com')
    count += 1
print(count)                            # 1
print()

#*************************
# modified code

addrs = "alpha@foo.com,beta@foo.com,gamma@foo.com,delta@foo.com"
print(len(addrs), type(addrs))          # 54 <class 'str'>

if isinstance(addrs, str):
    addrs = addrs.split(",")            # OK to change a str to a list
    print(len(addrs), type(addrs))      # 4 <class 'list'>

count = 0
for addr in addrs:
    print(addr)                         # alpha@foo.com
                                        # beta@foo.com
                                        # gamma@foo.com
                                        # delta@foo.com
    # code, resp = self.cmd('RCPT TO: <%s>' % addr)   # work, correct SMTP syntax resulting
    # we will have four self.cmd
    # code, resp = self.cmd('RCPT TO: alpha@foo.com')
    # code, resp = self.cmd('RCPT TO: beta@foo.com')
    # code, resp = self.cmd('RCPT TO: gamma@foo.com')
    # code, resp = self.cmd('RCPT TO: delta@foo.com')
    count += 1
print(count)                            # 4

@DuboisPa I think you are mistaking a comma-separated-string as a Python list.

addrs = "alpha@foo.com,beta@foo.com,gamma@foo.com,delta@foo.com"

If you could change above into a list, then it will work.

addrs = ["alpha@foo.com", "beta@foo.com", "gamma@foo.com", "delta@foo.com"]

in this case these lines are useless

if isinstance(addrs, str):
    addrs = [addrs]   

@DuboisPa The above code lets you use either a string or a list when there is only one single recipient,
smtp.to('a@mail.com') or smtp.to(['a@mail.com'])

It's a feature, not a bug.

:-)