邮件打开时收到消息

时间:2014-02-24 09:23:14

标签: python email

从下面的代码中,我发送的邮件包含一些信息。我需要将邮件发送到我的邮件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')

1 个答案:

答案 0 :(得分:2)

尝试添加此标题:

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

读者可能需要确认您收到回复。另外,添加服务器提供的html内容也可以选择识别邮件被读取。

您应该能够使用这些行中的任何一行

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