2016-08-31 10 views
0

私はsmtplibを使ってアラビア語とペルシア語の文字を含めて電子メールを送信しようとしています。Python - smtplibを使ってアラビア語の電子メールを送信

def send_email (admin, pwd, user, message): 
    server = smtplib.SMTP('smtp.gmail.com', 587) 
    server.ehlo() 
    server.starttls() 
    server.login(admin, pwd) 
    server.sendmail(admin, user, message) 
    server.close() 
    return True 

send_email('[email protected]', 'example', '[email protected]', 'کاراکتر فارسی و عربی Persian and Arabic Characters') 

と、私は次のエラーを取得する::以下は、私の機能である

msg = _fix_eols(msg).encode('ascii') 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) 

それを修正する方法上の任意のアイデア?

答えて

1

トライ.encode( 'UTF-8') はそれが

1

次のコードは、あなたの問題を解決する必要が助けいただければ幸いです:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import smtplib 
import email.mime.text 

    def send_email (admin, pwd, user, message): 
     server = smtplib.SMTP('smtp.gmail.com', 587) 
     server.ehlo() 
     server.starttls() 
     server.login(admin, pwd) 
     server.sendmail(admin, user, message) 
     server.close() 
     return True 

    msg = email.mime.text.MIMEText("پایتون", _charset="UTF-8") 
    print send_email('[email protected]', 'passwd', '[email protected]', msg.as_string())` 
関連する問題