通过Zoho从{Python发送电子邮件

时间:2016-03-31 19:23:49

标签: python imap zoho

我正在尝试从Zoho发送电子邮件,但是:有时我会收到电子邮件,有时候不会收到电子邮件,而且也会发送给其他人。

但在mail.zoho.com中,所有电子邮件都存在于SENT E-MAILS中,但有时人们没有收到,而且这不是垃圾邮件问题。

def sendMailTo(subject='', msgHTML='', to='', no_reply=False, mail_type=None):
    try:
        j = json.loads(msgHTML)
        msgHTML = ""
        for k in j.keys():
            msgHTML += str(k) + ": " + str(j[k])
            msgHTML += "\n"
    except ValueError:
        pass

     #Create the root message and fill in the from, to, and subject headers
    msg = MIMEMultipart('related')
    msg2 = MIMEText(msgHTML, 'html', 'UTF-8') #Content-Type: text/html; charset="us-ascii"
    msg.attach(msg2)

    # This example assumes the image is in the current directory
    import os
    os.chdir(os.path.dirname(__file__))
    #print(os.getcwd())
    fp = open('./utils/images/logo.png', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<logo>')
    msg.attach(msgImage)
    fp = open('./utils/images/facebook.png', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<facebook>')
    msg.attach(msgImage)

    recipients = [to]
    TO = ", ".join(recipients)
    msg['Subject'] = subject
    msg['To'] = TO

    if no_reply:
        FROM = "no-reply@xxxxx.com"
        msg['From'] = FROM
        # Send the message via our own SMTP server, but don't include the
        # envelope header.
        s = smtplib.SMTP_SSL('smtp.zoho.com', 465)
        # s.set_debuglevel(1)
        # s.starttls()
        s.ehlo()
        s.login(FROM, 'yoyoyoyoyoyo')
        s.sendmail(FROM, recipients, msg.as_string())
        s.quit()
    else:
        FROM = "xxxxxxxxx@gmail.com"
        msg['From'] = FROM
        # Send the message via our own SMTP server, but don't include the
        # envelope header.
        s = smtplib.SMTP('smtp.gmail.com', 587)
        #s.set_debuglevel(1)
        #s.ehlo()
        s.starttls()
        s.login(FROM, 'yoyoyoyoyoyo')
        s.sendmail(FROM, recipients, msg.as_string())
        s.quit()

0 个答案:

没有答案
相关问题