3

I have email address list as below:

email_list = [a@gmail.com, b@gmail.com, c@gmail.com]

I converted with below:

email_string = ''.join(str(e) for e in email_list)

But I got emails are a@gmail.com b@gmail.com c@gmail.com which can not be received email, I would like to receive email successfully(Exchange mail server).

my function is as below, I use Mailbox of exchangelib:

to_recipients=[Mailbox(email_address=' '.join(str(e) for e in list(test.user.all().values_list('email', flat=True))))]
Elsa
  • 1
  • 1
  • 8
  • 27
  • 1
    `Mailbox` takes only one email address. If you want to send to multiple recipients, you must use multiple mailboxes: `to_recipients=[Mailbox(email_address=dest) for dest in email_list]`. – DYZ Jun 12 '18 at 15:57
  • @DyZ Thank you again, I changed my code as you wrote, now I get a `ValueError` of `Mailbox must have either 'email_address' or 'item_id' set`, do you know what the matter is? Thank you so much, I am trapped in this for almost two days........ – Elsa Jun 12 '18 at 16:07
  • Surely it does have the email_address. Please update your code? – DYZ Jun 12 '18 at 16:09
  • my code is `to_recipients=[Mailbox(email_address=dest) for dest in list(course.student.all().values_list('email', flat=True))]` – Elsa Jun 12 '18 at 16:10
  • I executed `[Mailbox(email_address=dest) for dest in ['a@gmail.com', 'b@gmail.com', 'c@gmail.com']]` and it went well. I cannot reproduce your problem. I suggest that you rewrite your question to reflect the actual problem that you are facing now. – DYZ Jun 12 '18 at 16:16
  • It's too weird, I am sure list(course.student.all().values_list('email', flat=True)) is a correct list same as `['a@gmail.com', 'b@gmail.com', 'c@gmail.com']` , I printed the value just now. – Elsa Jun 12 '18 at 16:37
  • @DyZ I got it, I got the `ValueError:Mailbox must have either 'email_address' or 'item_id' set`, because of someone(the user) didn't set email address, I got a empty value, then I got the error.........New problem, haha, Thank you again, so so so much,❤️ – Elsa Jun 12 '18 at 16:42
  • Also, `to_recipients` will convert a simple list of `str` email adresses to `Mailbox` instances internally, so you can just do: `to_recipients=list(e for e in test.user.all().values_list('email', flat=True) if e)` – Erik Cederstrand Jun 13 '18 at 09:22
  • @ErikCederstrand Hi Erik I have to thank you after so many days, hahaha ☕️☕️, Wish you and your family all the best. – Elsa Dec 26 '19 at 06:28

2 Answers2

0

You have the right idea - just use a semicolon instead of an empty string when joining:

email_string = ';'.join(str(e) for e in email_list)
# Here ---------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank you so much for your prompt answer, I tried both `';'.join(str(e) for e in email_list)` and `' '.join(str(e) for e in email_list)`, receiving mails failed, it says wrong email address format......Do you know how to convert them – Elsa Jun 12 '18 at 15:35
  • @Begin2Pip Then your problem is not with concatenating strings. You do not use the concatenated string correctly. Please update the original post and show how you _actually_ attempt to send the email. – DYZ Jun 12 '18 at 15:36
  • 1
    @DyZ I thought it's about concatenating strings, I already clarified why I need this, right? If you don't know the answer, please leave it and do not answer. You don't need to send a negative vote. Thank you. – Elsa Jun 12 '18 at 15:40
  • It's not about _why_ you need it, it's about _how_ you use the concatenated string. Based on what you say, the concatenated string was not recognized as the address. I cannot answer your concern unless I see how you use the result strng. What function do you use to send the email? – DYZ Jun 12 '18 at 15:44
-1

While it appears that the apostrophes contain a space they actually do not.

email_string = ''.join(str(e) for e in email_list)

is different than

email_string = ' '.join(str(e) for e in email_list)
Vince Miller
  • 190
  • 1
  • 2
  • 15
  • @VinceMillerThank you so much for your prompt answer, I tried both ';'.join(str(e) for e in email_list) and ' '.join(str(e) for e in email_list), receiving mails failed, it says wrong email address format......Do you know how to convert them – Elsa Jun 12 '18 at 15:36