如何像gmail一样转发电子邮件?

2024-04-23 09:24:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有什么方法可以转发电子邮件吗。。。下面有附加文本)使用smtp? 下面是我的一段代码:

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_字符串()具有特殊标头和其他未使用的信息。 我猜gmail阻止它是因为这个。在


Tags: to方法clientidmessagedatastring电子邮件
1条回答
网友
1楼 · 发布于 2024-04-23 09:24:33

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

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!!')

相关问题 更多 >