예제 #1
0
def send_email_html(user, pwd, recipient, subject, html, showpicture):

    # me == my email address
    # you == recipient's email address
    gmail_user = user
    gmail_pwd = pwd
    me = user
    you = []
    for address in recipient.split(";"):
        you.append(address.strip())
    print " Sending mail to : ", recipient

    # Create message container - the correct MIME type is multipart/alternative.
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = me
    msg['To'] = ", ".join(you)
    #msg.preamble = 'Our family reunion'

    # Create the body of the message HTML version

    # Record the MIME t
    part1 = MIMEText(html, 'html')
    msg.attach(part1)

    if showpicture:
        #retrieve last picture ------------------------------------
        global MYPATH

        photolist = hardwaremod.photolist(MYPATH)
        imgfiles = []
        if photolist:
            referencestr = photolist[0][0].split(",")[0]
            for items in photolist:
                if referencestr in items[0]:
                    folderpath = os.path.join(MYPATH, "static")
                    folderpath = os.path.join(folderpath, items[0])
                    imgfiles.append(folderpath)

        for filename in imgfiles:
            # Open the files in binary mode.  Let the MIMEImage class automatically
            # guess the specific image type.
            print "filename ", filename
            fp = open(filename, 'rb')
            img = MIMEImage(fp.read())
            fp.close()
            picturename = os.path.basename(filename)
            img.add_header('Content-Disposition',
                           'attachment; filename="%s"' % picturename)
            msg.attach(img)

    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(me, you, msg.as_string())
        server.quit()
        print 'successfully sent the mail'
        logger.info('mail sent succesfully ')
        return True
    except:
        logger.error('failed to send mail')
        print "failed to send mail"
        return False
예제 #2
0
def send_email_html(user, pwd, recipient, subject, html, showpicture):

	# me == my email address
	# you == recipient's email address
	gmail_user = user
	gmail_pwd = pwd
	me = user
	you=[]
	for address in recipient.split(";"):
		you.append(address.strip())
	print " Sending mail to : ", recipient

	# Create message container - the correct MIME type is multipart/alternative.
	msg = MIMEMultipart()
	msg['Subject'] = subject
	msg['From'] = me
	msg['To'] =", ".join(you)
	#msg.preamble = 'Our family reunion'

	# Create the body of the message HTML version

	# Record the MIME t
	part1 = MIMEText(html, 'html')
	msg.attach(part1)
	
	if showpicture:
		#retrieve last picture ------------------------------------
		global MYPATH
			
						
		photolist=hardwaremod.photolist(MYPATH)
		imgfiles=[]	
		if photolist:
			referencestr=photolist[0][0].split(",")[0]
			for items in photolist:
				if referencestr in items[0]:
					folderpath=os.path.join(MYPATH, "static")
					folderpath=os.path.join(folderpath, items[0])
					imgfiles.append(folderpath)

	
		for filename in imgfiles:
			# Open the files in binary mode.  Let the MIMEImage class automatically
			# guess the specific image type.
			print "filename " , filename
			fp = open(filename, 'rb')
			img = MIMEImage(fp.read())
			fp.close()
			picturename=os.path.basename(filename)
			img.add_header('Content-Disposition','attachment; filename="%s"' % picturename)
			msg.attach(img)

	try:
		server = smtplib.SMTP("smtp.gmail.com", 587)
		server.ehlo()
		server.starttls()
		server.login(gmail_user, gmail_pwd)
		server.sendmail(me, you, msg.as_string())
		server.quit()
		print 'successfully sent the mail'
		logger.info('mail sent succesfully ')
		return True
	except:
		logger.error('failed to send mail')
		print "failed to send mail"
		return False