0

Is there any way to forward email(gmail like - you know... with additional text under) using smtp? Here is a piece of my code:

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

Now it's not working because message.as_string() has special headers and other unused info. I guess gmail blocked it because this.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Pavel Lankmiler
  • 327
  • 1
  • 2
  • 8

1 Answers1

0

below code worked for me correctly, even sending mail continuously on function call

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!!')
Gahan
  • 4,075
  • 4
  • 24
  • 44