Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何像gmail一样转发电子邮件?_Python_Email_Smtp_Gmail_Imaplib - Fatal编程技术网

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

Python 如何像gmail一样转发电子邮件?,python,email,smtp,gmail,imaplib,Python,Email,Smtp,Gmail,Imaplib,有没有办法使用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] messag

有没有办法使用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阻止它是因为这个

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

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

基本上,您希望从python代码发送电子邮件,对吗?您可以尝试清除标题信息,然后强制重新创建它。