[BangPypers] Change the "From:" address using python smtplib

Baskar K baskar.krishnan at jouve.in
Tue Oct 30 14:13:03 CET 2012


Hi, I am new to python, and I am writing the code to send a mail. It works
but i need to change the from address of the mail but it takes the
authentication mail id as from address. Please help me

    import smtplib, 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

    def send_mail(send_from, send_to, subject, text, files=[], server="
smtp.gmail.com:587"):
        assert type(send_to)==list
        assert type(files)==list

        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject

        msg.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))
            msg.attach(part)

        smtp = smtplib.SMTP(server)
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo
        smtp.login('baskar.krishnan at gmail.com', 'password')
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.close()

    files = ['Config.ini', 'mail_ini.txt']
    send_to = ['baskar.krishnan at gmail.com']
    send_mail('mahadevan.m at gmail.com', send_to, 'test subject', 'body
matter', files)

Thanks in advance
Baski


More information about the BangPypers mailing list