Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email to multiple recipients from .txt file with Python smtplib

Tags:

python

email

I try to send mails from python to multiple email-addresses, imported from a .txt file, I've tried differend syntaxes, but nothing would work...

The code:

s.sendmail('[email protected]', ['[email protected]', '[email protected]', '[email protected]'], msg.as_string())

So I tried this to import the recipient-addresses from a .txt file:

urlFile = open("mailList.txt", "r+")
mailList = urlFile.read()
s.sendmail('[email protected]', mailList, msg.as_string())

The mainList.txt contains:

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

But it doesn't work...

I've also tried to do:

... [mailList] ... in the code, and '...','...','...' in the .txt file, but also no effect

and

... [mailList] ... in the code, and ...','...','... in the .txt file, but also no effect...

Does anyone knows what to do?

Thanks a lot!

like image 804
Francis Michels Avatar asked Aug 04 '11 12:08

Francis Michels


People also ask

How do you send an email to multiple recipients in Python?

sendmail() function. In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.

How do I send an email to multiple recipients using SMTP?

For smtp you need to have port number, logon email address and in To:"[email protected];[email protected]" … try this out.

What methods can be used to send emails to multiple recipients?

You can send a mass email to more than one recipient using the BCC feature. Click the compose box, after composing your message, click on BCC and add all your recipients. This will send the emails to the recipients keeping email addresses hidden from each other.


1 Answers

This question has sort of been answered, but not fully. The issue for me is that "To:" header wants the emails as a string, and the sendmail function wants it in a list structure.

# list of emails
emails = ["[email protected]", "[email protected]", "[email protected]"]

# Use a string for the To: header
msg['To'] = ', '.join( emails )

# Use a list for sendmail function
s.sendmail(from_email, emails, msg.as_string() )
like image 122
Banjer Avatar answered Oct 04 '22 01:10

Banjer