【问题标题】:How to gmail like forward email?gmail如何转发电子邮件?
【发布时间】:2017-10-24 16:22:50
【问题描述】:

有没有什么方法可以使用 smtp 转发电子邮件(gmail 之类的 - 你知道......下面有额外的文字)? 这是我的一段代码:

def forward_email(email_id, email_to):
    client, messages = get_original_email(email_id)
    typ, data = client.fetch(messages[0].split(' ')[-1], '(RFC822)')
    email_data = data[0][1]
    message = email.message_from_string(email_data)

    smtp = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
    smtp.ehlo()
    smtp.starttls()
    smtp.login(IMAP_LOGIN, IMAP_PASSWORD)
    result = smtp.sendmail(email.utils.parseaddr(message['From']), 
        email_to, message.as_string())
    smtp.quit()
    return result

现在它不起作用,因为 message.as_string() 具有特殊的标头和其他未使用的信息。 我猜gmail阻止它是因为这个。

【问题讨论】:

  • 基本上你想从 python 代码发送电子邮件吧?
  • 您可以尝试清除标题信息然后强制重新创建。

标签: python email smtp gmail imaplib


【解决方案1】:

下面的代码对我来说是正确的,甚至在函数调用时连续发送邮件

def send_email(to='example@email.com', f_host='send.one.com', f_port=587, f_user='from@me.com', f_passwd='my_pass', subject='subject for email', message='content message'):
    smtpserver = smtplib.SMTP(f_host, f_port)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(f_user, f_passwd) # from email credentialssssssssss
    header = 'To:' + to + '\n' + 'From: ' + f_user + '\n' + 'Subject:' + subject + '\n'
    message = header + '\n' + message + '\n\n'
    smtpserver.sendmail(f_user, to, str(message))
    smtpserver.close()
    DBG('Mail is sent successfully!!')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2022-06-17
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2013-08-11
    相关资源
    最近更新 更多