2

I need to send a certain email to a lot of different addresses, one recipient at a time. It has an attachment.

So far, I've programmed this as such :

1) create a Thread object per mail-address (looping through the recipients list).

2) within each Thread object, create the MIMEMultipart() message.

3) within each Thread object, send the mail through smtplib.SMTP("smtp.gmail.com:587")

It's working fine.

The problem with this approach is that the attachment has to be attached separately for every single email. Is there a way to only attach it once ? A global MIMEMultipart() message is not possible because different threads would have to change it (to change the recipient's address).

S Leon
  • 331
  • 1
  • 4
  • 18

3 Answers3

1

Just don't send individual e-mails. The only valid reason for that is so the each receiver does not see the all of the other e-mail addresses. Blind-copy (BCC) was invented for that.

See this, for example.

The other thing you need to know is that most e-mail providers take some measure to inhibit spamming. The techniques vary, but sending to too many unreachable addresses is red-flagged. Likewise, some limit the number of identical emails. This varies by provider.

Why do they do this? Because the major players all basically agree that mass e-mailing is something to inhibit. So they "guard against each other by refusing or blocking offenders".

Good luck. Conceptually it is pretty simple, and if your volume is low and the email addresses are good, you should do ok. Prune out bad emails!

When you get replies of undeliverable messages, delete the addresses from your list.

Community
  • 1
  • 1
Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29
  • 1
    Making recipients feel personally targeted (as opposed to being part of a big list, or being in a BCC), is very valuable. My message certainly isn't any spam, and will interest most of the (80) receivers. I just want to avoid giving the impression of being part of a mass group. – S Leon Mar 26 '15 at 23:23
  • Of course, your message isn't spam - to you or all 80 of your receivers. But it has to go through an ISP that can't know whether your receivers like it and does not care what you think of it. :) That aside, assume that you could do what you want. How does it help? You have 80 threads all competing for access to a single resource, a port on a single smtp server. Will that server load balance all of your requests? My guess is that regardless of whether you multiprocess, or thread, you will still get sequential performance. You can test this by sending n copies to yourself. – Fred Mitchell Mar 28 '15 at 01:11
  • I forgot to mention. If you use the BCC approach, each smtp provider has its own upper limit on email addresses per message. – Fred Mitchell Mar 28 '15 at 01:25
1

I was in the same situation before. I can advise using yagmail. I'm the developer/maintainer at github.

Last week I added the option to add attachments, today I added caching possibility.

What this means is that you can get it to cache all objects. HTML, images etc. It would make the MIMEImage once, and then reuse it everytime.

For example, setting up the connection:

from yagmail import Connect
yag = Connect('user', 'pw')

Then sending a lot of emails:

img_path = '/path/to/local/image.png'
for name in ['Hans', 'David', 'Taco', 'Dori']:
    target = '{}@gmail.com'.format(name)
    yag.send(target, Subject = 'Hi ' + name, useCache = True
             Contents = ['Pre text', img_path , 'Some post text'])

It will automatically understand that img_path is pointing to a local image, make a MIMEImage object of it, and then cache it under the name of the img_path. Every time you refer to the image, it will use the preloaded MIME object.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
0

The other approach is to use an outside service, such as icontact or mailchimp to manage your important messages. That can be done on a cloud based service, and can provide legal compliance. With some, you can even determine whether the mail has been read!

They might even be able to set up opt-in SMS for you to use with your clients.

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29