Example #1
0
def main(aPicTaker, aMsg):
	print("Starting to Send Email")
	#Define time stamp & record an image
	pic_time = datetime.now().strftime('%Y%m%d%H%M%S')
	#command = 'raspistill -w 1280 -h 720 -vf -hf -o '+pic_time+'.jpg'
	#os.system(command)
	print("About to save Image")
	aPicTaker.saveIm(pic_time+'.jpg')
	print("Image Saved")
	#Email information
	smtpUser = "******" # ADD EMAIL ADDRESS
	smtpPass = #ADD PASSWORD

	#Destination Email Information

	toAdd = '*****@*****.**'
	fromAdd = smtpUser
	subject = 'Image Recorded at: ' + pic_time
	print("About to hit and multipart")
	msg = MIMEMultipart()
	msg['Subject'] = subject
	msg['From'] = fromAdd
	msg['To'] = toAdd
	msg.preable = subject

	#Email Text
	body = MIMEText(aMsg)
	msg.attach(body)

	#Atach image
	fp = open(pic_time + '.jpg','rb')
	img = MIMEImage(fp.read())
	fp.close()
	msg.attach(img)
	
	# Send email
	print("About to send email")
	s = smtplib.SMTP('smtp.gmail.com', 587)
	print("Before ehlo")
	s.ehlo()
	print("before startttls")
	s.starttls()
	print("before 2nd ehlo")
	s.ehlo()

	print("Before Login")
	s.login(smtpUser, smtpPass)
	print("Before SendMail")
	s.sendmail(fromAdd, toAdd, msg.as_string())
	print("Before Quit")
	s.quit()

	print("Email delivered!")
Example #2
0
    def _send(self):
        """Send a mail for each shot in the shot list."""
        if not self.model.recipients:
            self.view.displayWarning(
                'No recipients', 'No recipients have been set for the email.')
            return

        if not (self.model.assets or self.model.tasks):
            self.view.displayWarning(
                'No asset or tasks',
                'No asset or task have been set for the email.')
            return

        recipients = self.model.recipients
        recipients.append(self.model.sender)

        s = smtplib.SMTP(Settings.MAIL_SERVER)

        # Create the root message
        msg_root = MIMEMultipart('related')
        msg_root['Subject'] = self.model.subject
        msg_root['From'] = self.model.sender
        msg_root['To'] = ', '.join(recipients)
        msg_root.preable = 'This is a multi-part message in MIME format.'

        msg_alternative = MIMEMultipart('alternative')
        msg_root.attach(msg_alternative)

        images = [
            Settings.IMAGE_MAIL_TEMPLATE.format(
                image_name='image_{0}'.format(i))
            for i in range(len(self.model.images))
        ]

        msg_alternative.attach(MIMEText(self.model.plain_text, 'plain'))
        msg_alternative.attach(MIMEText(self.model.getHtml(images), 'html'))

        for i, path in enumerate(self.model.images):
            with open(path, 'rb') as image:
                msg_image = MIMEImage(image.read())
                msg_image.add_header('Content-ID', '<image_{0}>'.format(i))
                msg_root.attach(msg_image)

        s.sendmail(self.model.sender, recipients, msg_root.as_string())
        s.quit()

        return True