Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python script for SMTP-sending files given in the commandline

1 view
Skip to first unread message

Nomen Nescio

unread,
Sep 13, 2008, 11:00:04 AM9/13/08
to
Archive-name: python-mailer

Below is the public domain code of the Python script able
to send all files defined in the command line.
You may need to adjust some lines to match your environment, e.g.:

server=smtplib.SMTP('localhost')
msg['From']='sender@localhost'
msg['To']='reci...@gmail.com'
server.sendmail("sender@localhost","reci...@gmail.com",msg.as_string())

#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.6.3).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `#!/bin/sh' line above, then type `sh FILE'.
#
lock_dir=_sh20518
# Made on 2008-09-13 15:57 CEST by <user@nowhere>.
# Source directory was `/tmp'.
#
# Existing files will *not* be overwritten, unless `-c' is specified.
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 1119 -rw-r--r-- file_send.py
#
MD5SUM=${MD5SUM-md5sum}
f=`${MD5SUM} --version | egrep '^md5sum .*(core|text)utils'`
test -n "${f}" && md5check=true || md5check=false
${md5check} || \
echo 'Note: not verifying md5sums. Consider installing GNU coreutils.'
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
if test "$gettext_dir" = FAILED && test -f $dir/gettext \
&& ($dir/gettext --version >/dev/null 2>&1)
then
case `$dir/gettext --version 2>&1 | sed 1q` in
*GNU*) gettext_dir=$dir ;;
esac
fi
if test "$locale_dir" = FAILED && test -f $dir/shar \
&& ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
then
locale_dir=`$dir/shar --print-text-domain-dir`
fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
echo=echo
else
TEXTDOMAINDIR=$locale_dir
export TEXTDOMAINDIR
TEXTDOMAIN=sharutils
export TEXTDOMAIN
echo="$gettext_dir/gettext -s"
fi
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null
then if (echo -n test; echo 1,2,3) | grep n >/dev/null
then shar_n= shar_c='
'
else shar_n=-n shar_c= ; fi
else shar_n= shar_c='\c' ; fi
f=shar-touch.$$
st1=200112312359.59
st2=123123592001.59
st2tr=123123592001.5 # old SysV 14-char limit
st3=1231235901

if touch -am -t ${st1} ${f} >/dev/null 2>&1 && \
test ! -f ${st1} && test -f ${f}; then
shar_touch='touch -am -t $1$2$3$4$5$6.$7 "$8"'

elif touch -am ${st2} ${f} >/dev/null 2>&1 && \
test ! -f ${st2} && test ! -f ${st2tr} && test -f ${f}; then
shar_touch='touch -am $3$4$5$6$1$2.$7 "$8"'

elif touch -am ${st3} ${f} >/dev/null 2>&1 && \
test ! -f ${st3} && test -f ${f}; then
shar_touch='touch -am $3$4$5$6$2 "$8"'

else
shar_touch=:
echo
${echo} 'WARNING: not restoring timestamps. Consider getting and'
${echo} 'installing GNU `touch'\'', distributed in GNU coreutils...'
echo
fi
rm -f ${st1} ${st2} ${st2tr} ${st3} ${f}
#
if test ! -d ${lock_dir}
then : ; else ${echo} 'lock directory '${lock_dir}' exists'
exit 1
fi
if mkdir ${lock_dir}
then ${echo} 'x - created lock directory `'${lock_dir}\''.'
else ${echo} 'x - failed to create lock directory `'${lock_dir}\''.'
exit 1
fi
# ============= file_send.py ==============
if test -f 'file_send.py' && test "$first_param" != -c; then
${echo} 'x -SKIPPING file_send.py (file already exists)'
else
${echo} 'x - extracting file_send.py (text)'
sed 's/^X//' << 'SHAR_EOF' > 'file_send.py' &&
#!/usr/bin/python
import smtplib
from email import Encoders
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
X
import sys
arguments=""
pliki="\n"
for l in sys.argv[1:]:
X arguments+=l.strip()
X pliki+=" "+l.strip()+"\n"
server=smtplib.SMTP('localhost')
server.set_debuglevel(0)
msg = MIMEMultipart()
msg['Subject']="files: "+arguments+","
msg['From']='sender@localhost'
msg['To']='reci...@gmail.com'
msg.preamble='Files sent:\n'+pliki
X
for plk in sys.argv[1:]:
X print plk
X # Open the files in binary mode. Let the MIMEImage class automatically
X # guess the specific image type.
X ctype = 'application/octet-stream'
X maintype, subtype = ctype.split('/', 1)
X fp = open(plk, 'rb')
X app = MIMEBase(maintype, subtype)
X app.set_payload(fp.read())
X fp.close()
X # Encode the payload using Base64
X Encoders.encode_base64(app)
X # Set the filename parameter
X app.add_header('Content-Disposition', 'attachment', filename=plk)
X msg.attach(app)
X
server.sendmail("sender@localhost","reci...@gmail.com",msg.as_string())
server.quit()
X
X
SHAR_EOF
(set 20 08 09 13 15 57 20 'file_send.py'; eval "$shar_touch") &&
chmod 0644 'file_send.py'
if test $? -ne 0
then ${echo} 'restore of file_send.py failed'
fi
if ${md5check}
then (
${MD5SUM} -c >/dev/null 2>&1 || ${echo} 'file_send.py: MD5 check failed'
) << SHAR_EOF
118bfd7af9c8c5698041a0bb14a6a286 file_send.py
SHAR_EOF
else
test `LC_ALL=C wc -c < 'file_send.py'` -ne 1119 && \
${echo} 'restoration warning: size of file_send.py is not 1119'
fi
fi
if rm -fr ${lock_dir}
then ${echo} 'x - removed lock directory `'${lock_dir}\''.'
else ${echo} 'x - failed to remove lock directory `'${lock_dir}\''.'
exit 1
fi
exit 0

0 new messages