Hi.
I made a script that sends the bugfixed(made by user) tcz by Python.
Please examine this. Request for comments.
#!/bin/sh -e
# restcz.sh
# Send a bugfixed tcz to the e-mail addr.
# 2012 Akane mail:i.can.speak.c.and.basic@gmail.com
# How to use:
# $ ./restcz.sh [tcz archive file]
# e.g.,$ ./restcz.sh gfortran.tar.gz.bfe
#
################################
# NOTICE. READ BEFORE RUNNING. #
#################################################
# The e-mail addr which is to send bugfixed tcz #
# is "tcesubmit _at_ gmail _dot_ com". #
# So please subst it to the "to_addr" below. #
#################################################
normalsmtp=\"smtp.anywhere.com\"
googlesmtp=\"smtp.gmail.com:587\"
yourgmailuser="getpass.getuser()"
yourgmailpasswd="getpass.getpass()"
# Uncomment these if needed.
#from_addr="you@anywhere.com"
# change _at and _dot_ before uncommenting
#to_addr="tcesubmit _at_ gmail _dot_ com"
if ! which python >/dev/null;then
echo install python.tcz first. >&2
exit 1
fi
usage(){
cat <<END >&2
$@.
Usage: $0 [file to mail]
END
exit 1
}
[ $# -ne 1 ] && usage invalid number of the argument
sendfile=$1
[ -e "$sendfile" ] || usage $sendfile does not exists
echo $sendfile | grep \.tar\.gz\.bfe >/dev/null || \
[ $0 = $sendfile ] || \
usage $sendfile is not a tcz archive
basesendfile=$(basename $sendfile)
subject="$(basename $sendfile .tar.gz.bfe).tcz update"
if [ -z "$to_addr" ];then
echo -n input the e-mail addr to send $sendfile:
read to_addr
fi
if [ -z "$from_addr" ];then
echo -n input your e-mail addr:
read from_addr
fi
echo input the body of the mail. press ^D to exit editing.
body=$(cat)
lessthem(){
less <<END
Things will be sent.
Subject: $subject
To: $to_addr
From: $from_addr
Attach: $sendfile
Message:
$body
END
}
while ! [ "$ans" = y ];do
lessthem
echo -n Is this OK? \(y,n,r\)
read ans
if [ "$ans" = n ];then
cat <<END
You chose n. Now exit.
Writing such data on this script is recommended.
It will be easy for you!
END
exit 2
fi
done
echo sending a mail to $to_addr. this may take some time.
/usr/bin/env python <<END
import os.path
import datetime
import smtplib
import getpass
from email import Encoders
from email.Utils import formatdate
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def create_message(from_addr, to_addr, subject, body, attach_file):
'''
Build the mail data.
'''
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to_addr
msg["Date"] = formatdate()
body = MIMEText(body)
msg.attach(body)
# set the MIME type of the attachment
#(actually, this is not needed)
attachment = MIMEBase("Unknown","Unknown")
# set the data of attachment
file = open(attach_file)
attachment.set_payload(file.read())
file.close()
Encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition", \
"attachment", filename=os.path.basename(attach_file))
return msg
def send_by_google_smtp(from_addr, to_addr, msg):
"""
Send a mail by using gmail smtp server.
"""
username=$yourgmailuser
password=$yourgmailpasswd
server = smtplib.SMTP($googlesmtp)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
def send_by_normal_smtp(from_addr, to_addr, msg):
"""
Send a mail by using normal(don't use TLS) smtp server.
"""
smtp = smtplib.SMTP($normalsmtp)
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.close()
if __name__ == '__main__':
from_addr = "$from_addr"
to_addr = "$to_addr"
subject = "$subject"
body = "$body"
sendfile = "$sendfile"
msg = create_message(from_addr, to_addr, \
subject, body, sendfile)
send_by_google_smtp(from_addr, to_addr, msg)
END
echo done. thank you for using this script. by Akane.