Skip to content

Instantly share code, notes, and snippets.

@brodul
Created October 10, 2013 14:49
Show Gist options
  • Save brodul/6919615 to your computer and use it in GitHub Desktop.
Save brodul/6919615 to your computer and use it in GitHub Desktop.
"""
You will need::
- an activated virtualenv with celery installed
- a rabbitmq on localhost running
There must be a running celery worker running::
celery -A tasks worker --loglevel=info &
This script will send 20 mails to random emails on the mailinator.com service you can then
view the inbox on the provided url.
With the default configuration celery worker will accept 4 parallel task.
And you can send more mails in a bulk mailer in one task.
"""
import time
import random
import string
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# import celery
from celery import Celery
celery = Celery('tasks', backend='amqp', broker='amqp://')
@celery.task
def blocking_sleep(sec):
time.sleep(sec)
def generate_mail():
char_set = string.ascii_uppercase + string.digits
random_name = ''.join(random.sample(char_set*6,6))
print 'http://mailinator.com/inbox.jsp?to=' + random_name
return random_name + '@mailinator.com'
@celery.task
def send_mail(to=None, text=None, sub=None, from_=None):
text = text or "Test body."
sub = sub or str(time.time())
from_ = from_ or "[email protected]"
to = to or generate_mail()
msg = MIMEText("test")
msg['Subject'] = sub
msg['From'] = from_
msg['To'] = to
s = smtplib.SMTP('mailinator.com')
s.sendmail(from_, [to], msg.as_string())
s.quit()
return to
if __name__ == "__main__":
for i in range(20):
# delay makes it async
mail = generate_mail()
send_mail.delay(mail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment