torsten: utils/testing massmail2.py,NONE,1.1
cvs at intevation.de
cvs at intevation.de
Fri Apr 7 13:55:46 CEST 2006
Author: torsten
Update of /kolabrepository/utils/testing
In directory doto:/tmp/cvs-serv22290
Added Files:
massmail2.py
Log Message:
Added an improved version of the mass_mail.py script. In contrast to the old script it supports generating attachements of random size.
--- NEW FILE: massmail2.py ---
#!/usr/bin/python
"""\n
Sends multiple (massiv) email to a given email address. Dummy files could
be attached as an option. On default no files are attached.
Mails will be send with massmailer at example.tld using localhost as mailserver
if not specified otherwise. Subject of the mail is autogenerated through a
random number.
Usage: massmail mailaddress
Options:
-h / --help Print this message and exit.
-f from email address
--from=mailaddress
use a specific mailaddress as sender
-s mailserver
--server=mailserver
use a specifiv mailserver.
-a yes i want to attach dummy files
-n num
--num-email=num
How many emails should be send? 1 email is
default
--file-sizemax
maximum size in kb of random attachement
default is 1000KB
--file-sizemin
minimum size in kb of random attachement
default is 1KB
"""
# initial 20030723 Bernhard <bernhard at intevation.de>
# tuned 20060403 Torsten Irlaender <torsten.irlaender at intevation.de>
#
# This program is free software under the GNU GPL (>=v2)
# Read the file COPYING coming with the software for details.
import smtplib
import os
import getopt
import sys
from random import *
import random
import linecache
import linecache
# For guessing MIME type based on file name extension
import mimetypes
from email import Encoders
from email.Message import Message
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
mail_body_source='./tpgth10.txt'
verbose=0
def usage(code, msg=''):
print >> sys.stderr, __doc__
if msg:
print >> sys.stderr, msg
sys.exit(code)
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'has:f:t:n:', ['help','num-email=','file-sizemin=','file-sizemax=','to=','from=','server='])
except getopt.error, msg:
usage(1, msg)
server='127.0.0.1'
rcpt_to=''
mail_from='massmailer at example.tld'
num_mails=1
attach=0
fsmin=1
fsmax=1000
global verbose
dir = os.curdir
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-f','--from'):
mail_from = arg
elif opt in ('-s','--server'):
server = arg
elif opt =='-a':
attach=1
elif opt in ('-n','--num-email'):
num_mails = arg
elif opt == '--file-sizemax':
fsmax = arg
elif opt == '--file-sizemin':
fsmin = arg
if len(args) < 1:
usage(1)
rcpt_to = args[0]
print('Will send %s mails to %s from %s over server %s'% (num_mails,rcpt_to,mail_from,server))
i = 0
while i < int(num_mails):
build_mail(num_mails,rcpt_to,mail_from,server,fsmax,fsmin,attach)
if verbose:
print('%i. ' %i)
elif not verbose:
print('.'),
i= i+1
def build_mail(num_mails,rcpt_to,mail_from,server,fsmax,fsmin,attach):
# Create the container (outer) email message.
msg_mail = MIMEMultipart()
#create an random subject
r1 = randint(0,1000)
r2 = randint(0,1000)
r3 = randint(0,1000)
#msg['Subject'] = 'Massmail'
msg_mail['Subject'] = ("Massmail %i%i%i" %(r1,r2,r3))
msg_mail['From'] = mail_from
msg_mail['To'] = rcpt_to
msg_mail.preamble = 'This mail was sent by massmail'
#write messagebody
if verbose:
print('writing message body... '),
msg_mail.attach( MIMEText(random_passage()) )
if verbose:
print('Done.')
#Do we want a file
if attach:
if verbose:
print('generating file... '),
#msg_mail = attach_file(msg_mail)
msg_mail = attach_file(msg_mail,fsmin,fsmax)
if verbose:
print('Done')
# Guarantees the message ends in a newline
msg_mail.epilogue = ''
if verbose:
print('sending... '),
send_mail(msg_mail,mail_from,rcpt_to,server)
if verbose:
print('Done.')
def random_passage():
linewindow_max=42
numbers_of_lines= 2250
s=""
l=random.randrange(linewindow_max)
start=random.randrange(numbers_of_lines-l)
for i in range(start,start+l):
s=s+linecache.getline(mail_body_source, i)
return s
def attach_file(msg,fsmin,fsmax):
#create trashfile of random size and attach it to the mail
size = randint(int(fsmin),int(fsmax))
if verbose:
print('(%ikb) '),
filename = 'tempfile'
dummy = os.system("dd if=/dev/zero of=%s bs=1024 count=%i" % (filename,size))
fp = open(filename, 'rb')
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
msg_inner = MIMEBase(maintype, subtype)
msg_inner.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
Encoders.encode_base64(msg_inner)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(msg_inner)
return msg
def send_mail(msg,mail_from,rcpt_to,server):
# Send the email via our own SMTP server.
s = smtplib.SMTP(server)
#s.set_debuglevel(1)
try:
s.sendmail(mail_from, rcpt_to, msg.as_string())
except:
sys.stderr.write("Sorry something went wrong on sending email!\nMaybe retry with different mailaddresses.")
raise
#sys.exit(2)
s.close()
if __name__ == '__main__':
main()
More information about the commits
mailing list