I found that a python script gave me more flexibility in what I could
attach to an email...


import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

myRelease = os.environ.get("BUILD_STRING")
files=["myData.xml","myDataNightly_Previous.xml","myDataRelease_Previous.xml",
"myReport.html"]
to=["an.ema...@address.com","an.ema...@address.com","an.ema...@address.com"]
text1 = "To everyone\n\nmyData.xml has changed following the latest
build :- "
text2 = myRelease
text3 = "\r\n\nSee attached files\r\n\r\nReleases are in\"\\\\netdrive
\Product source code\Tagged\", \r\n\nNightly files are in \"\\\
\netdrive\Product source code\Nightly\"\r\n\nRegards,\r\n\nJenkins\r
\n"
text = text1+text2+text3

# Send the email via our own SMTP server.
def send_mail(to, subject, text, fromWho="", files=[], cc=[], bcc=[],
server="10.192.00.000"):
    assert type(to)==list
    assert type(files)==list
    assert type(cc)==list
    assert type(bcc)==list

    message = MIMEMultipart()
    message['From'] = fromWho
    message['To'] = COMMASPACE.join(to)
    message['Date'] = formatdate(localtime=True)
    message['Subject'] = subject
    message['Cc'] = COMMASPACE.join(cc)

    message.attach(MIMEText(text))

    for f in files:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(f, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment;
filename="%s"' % os.path.basename(f))
        message.attach(part)

    addresses = []
    for x in to:
        addresses.append(x)
    for x in cc:
        addresses.append(x)
    for x in bcc:
        addresses.append(x)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(fromWho, addresses, message.as_string())
    smtp.close()

send_mail(to, "myData.xml", text, "jenkins", files)

Reply via email to