1

Hi i am fetching email to list from database which is like below

'[email protected],[email protected],[email protected]'

i want to separate these mail like

 '[email protected]','[email protected]','[email protected]'

in python function so can pass it in msg["To"] field.

Rahul Gour
  • 487
  • 2
  • 7
  • 21

3 Answers3

0

You can use split function with delimeter as ,:

email_string = '[email protected],[email protected],[email protected]'
email_list = email_string.split(",")  
Kapil
  • 459
  • 3
  • 14
0

Use str.split like so

'[email protected],[email protected],[email protected]'.split(',')

outputs:

 ['[email protected]', '[email protected]', '[email protected]']
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
0

The msg["To"] parameter normally takes addresses separated by comma and a space so the following should be what you need:

email_string = '[email protected],[email protected],[email protected]'
msg["To"] = ', '.join(email_string.split(',')

This would pass the following single string:

[email protected], [email protected], [email protected]

or alternatively, you could use a replace to add a space:

msg["To"] = email_string.replace(',', ', ')

If you want your string to have quotes around each email address:

email_string = "'{}'".format("','".join(email_string.split(',')))

Giving you a string looking like:

'[email protected]','[email protected]','[email protected]'
Martin Evans
  • 45,791
  • 17
  • 81
  • 97