Skip to main content
 首页 » 编程设计

python之当邮件打开时收到消息

2024年11月01日4wuhuacong

从下面的代码中,我正在发送带有一些信息的邮件。这里我需要将消息发送到我的邮件 ID,当他/她打开邮件时。 如何在 Python 中执行此操作。

def Data(request): 
    templateName = "sendmail.html" 
    mapDictionary = {'fromMail': "xxxx.xxxx@gmail.com", 'password': "xxxxx", 'toMail': "yyyyy.yyyy@gmail.com@gmail.com", 
                        "subject": "New Trip Confirmation", 'username': 'Ramesh','trip_start_date' : '2014-02-10', 
                        'trip_start_place' : 'Visak', 'trip_start_time' : '11:00 AM', "templateName" : templateName 
                    } 
    print ("call send mail from processNewTripData...") 
    return sendmail(request, mapDictionary) 
 
 
def sendmail(request, mapDictionary): 
    try: 
        server = smtplib.SMTP('smtp.gmail.com',587) 
        server.starttls() 
        server.login(mapDictionary["fromMail"],mapDictionary["password"]) 
        context = Context(mapDictionary) 
        html_content = render_to_string(mapDictionary["templateName"], context)    
        #text_content = "This is Confirmation mail"        
        msg = MIMEText(html_content,'html') 
        msg['Subject'] = mapDictionary["subject"] 
        server.sendmail(mapDictionary["fromMail"], mapDictionary['toMail'], msg.as_string()) 
        to_json = {'result' : "True"} 
        return HttpResponse(simplejson.dumps(to_json), content_type='application/json') 
    except Exception as e: 
        print str(e) 
    to_json = {'result' : "False"} 
        return HttpResponse(simplejson.dumps(to_json), content_type='application/json') 

请您参考如下方法:

尝试添加这个标题:

Disposition-Notification-To: "User" <user@user.com> 

读者可能需要确认您是否收到回复。此外,添加由您的服务器提供的 html 内容可以作为一种识别邮件已读的选项。

您应该能够使用这些行中的任何一行来执行此操作

msg['Disposition-Notification-To'] = '"User" <user@user.com>' 
msg['Disposition-Notification-To'] = 'user@user.com'