mailing.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:vit_acad_web_api 作者: piyushrungta25 项目源码 文件源码
def send_mail(subject,body,reciever):


    with open(config_file) as fp:
        lines = fp.readlines()
    first_line_to_read = [i for i, line in enumerate(lines) if 'MAILING_DETAILS:' in line][0] + 1
    last_line_to_read = [i for i, line in enumerate(lines) if 'EOF' in line][0]

    for name in lines[first_line_to_read:last_line_to_read]:
        name=name.split(':')
        if name[0]=='SENDER_USERNAME':
            sender=name[1].split('\n')[0]
        elif name[0]=='SENDER_PASS':
            password=name[1].split('\n')[0]

    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = reciever
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))


    try:
        smtpObj = smtplib.SMTP('smtp.gmail.com:587')
        smtpObj.starttls()
        smtpObj.login(sender,password)
        smtpObj.sendmail(sender, reciever, msg.as_string())

    except smtplib.SMTPConnectError:
        msg='Mailing Error : error occured during connection with server.'


    except smtplib.SMTPAuthenticationError:
        msg='Mailing Error: Invalid username and/or password'


    except smtplib.socket.gaierror:
        msg='Mailing Error : Check Internet connection.'

    except Exception as e:
        msg='Mailing Error: Try again'

    else:
        msg="Admin has been successfully notified of the error via email."
        smtpObj.quit()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号