Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Attachment in email in python.  (gelezen 1610 keer)

Attachment in email in python.
« Gepost op: 2015/03/19, 14:38:40 »
Ik heb een python script voor het verzenden van een e-mail

import smtplib

fromaddr = 'henk@oegema.com'
toaddrs  = 'xxxxxx@gmail.com'
msg = 'Deur alarm!'


# Credentials
username = 'my username'
password = 'my password'

# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Wat ik hier aan wil toevoeging is een attachment van een foto (jpg).

 Wie kan mij daar mee helpen ?
Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)

Offline Johan van Dijk

  • Administrator
    • johanvandijk
Re: Attachment in email in python.
« Reactie #1 Gepost op: 2015/03/19, 20:59:59 »
Voorbeelden staan hier.
Nog meer kan je hier vinden: https://duckduckgo.com/?q=python%20smtplib%20examples

Re: Attachment in email in python.
« Reactie #2 Gepost op: 2015/03/19, 21:47:44 »
Voorbeelden staan hier.
Nog meer kan je hier vinden: https://duckduckgo.com/?q=python%20smtplib%20examples

Dank je Johan.

Eerlijk gezegd, had ik de voorbeelden van  https://docs.python.org/2/library/email-examples.html  zelf ook al gezien.

Echter,  ik zag (zie) door de bomen het bos niet meer.    ;)

Zal toch  maar wat tijd vrij maken om me er nog verder in te verdiepen.

 
Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)

Re: Attachment in email in python.
« Reactie #3 Gepost op: 2015/03/20, 00:15:49 »
Ik volg het voorbeeld van site:   https://docs.python.org/2/library/email-examples.html
(3e voorbeeld:   Here’s an example of how to send a MIME message containing a bunch of family pictures that may be residing in a directory:)

Daar staat:# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)


Ik heb het aangepast tot:
.............
for file in png files:
fp = open(file, 'rb')
img = MIMEImage(fp.read())
        fp.close()
        msg.attach(img)


Blijkbaar is deze syntax niet juist, want ik krijg:

henkoegema@henkoegema-MS-7708:~$ python mime_send_email.py
  File "mime_send_email.py", line 21
    for file in png files:
                        ^
SyntaxError: invalid syntax
henkoegema@henkoegema-MS-7708:~$



« Laatst bewerkt op: 2015/03/20, 00:33:12 door henkoegema »
Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)

Re: Attachment in email in python.
« Reactie #4 Gepost op: 2015/03/20, 02:12:25 »
De syntax is:

for <variabele> in <object>:
Waar <object> een iterable is. Uw code geeft twee waardes voor <object>, namelijk "png" en "files". Misschien is dit een typo aangezien het voorbeeld het over "pngfiles" heeft?

Maar uit uw beginpost begrijp ik dat het maar 1 foto is, dan is een for-loop helemaal niet nodig. Defnieer gewoon het pad naar het gewenste bestand:

    image_path = "/home/user/Afbeeldingen/..."
    fp = open(image_path, "rb")
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)
« Laatst bewerkt op: 2015/03/20, 02:14:28 door Nunslaughter »

Re: Attachment in email in python.
« Reactie #5 Gepost op: 2015/03/20, 13:03:19 »
De syntax is:

for <variabele> in <object>:
Waar <object> een iterable is. Uw code geeft twee waardes voor <object>, namelijk "png" en "files". Misschien is dit een typo aangezien het voorbeeld het over "pngfiles" heeft?

Maar uit uw beginpost begrijp ik dat het maar 1 foto is, dan is een for-loop helemaal niet nodig. Defnieer gewoon het pad naar het gewenste bestand:
...................................
..................................

Dank voor je antwoord.
Ik was in de veronderstelling dat dat "pngfiles" zoals dat in het voorbeeld staat fout was, omdat ik dat ook niet aan de gang kreeg.
Maar zoals ik het probeerde kan het dus helemaal niet werken.

De for-loop was alleen maar als test bedoeld, zodat ik het later kan aanpassen naar mijn situatie.
Maar......dat heb jij al voor me gedaan.   =D

Heb het nog niet geprobeerd, maar ga er snel mee aan de gang.
De uitkomst laat ik weten.   :)


Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)

Re: Attachment in email in python.
« Reactie #6 Gepost op: 2015/03/20, 15:02:58 »
Dankzij  jouw voorbeeld Johan is het gelukt.   =D

Het ziet er nu zo uit:
# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'GPIO Alarm'

msg['From'] = 'henk@oegema.com'
msg['To'] = 'xxxxxxxx@xxxxxx'
msg.preamble = 'Dit is een test'

username = 'myusername'
password = 'mypassword'

# Open the files in binary mode.  Let the MIMEImage class automatically
# guess the specific image type.
image_path = '/home/henkoegema/ff.png'
fp = open(image_path, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)

# Send the email via the Gmail server.
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(username,password)
s.sendmail('henk@oegema.com', 'xxxxxxx@xxxx.com', msg.as_string())
s.quit()
Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)

Re: Attachment in email in python.
« Reactie #7 Gepost op: 2015/03/20, 16:33:22 »
Voor  iemand die geïnteresseert is wat ik met de e-mail (met attachment) gedaan heb (gewoon als spielerei):

Op m'n Rasberrypi2,  als GPIO19 aan ground (aarde) wordt gelegd (via een uitwendige gebeurtenis ) zoals b.v. een deuralarm of bewegings sensor, etc.  dan
neemt de raspberry pi camera een snapshot en emailt die dan naar mijn emailadres.   

#!/usr/bin/python

# this Python application turns a Raspberry Pi into a security camera system
# it requires that you have a Pi camera installed

import datetime
import smtplib
import time
import picamera
import RPi.GPIO as GPIO
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# define the GPIO port you will use for the door sensor
SENSOR = 19

now = datetime.datetime.now()

# number of seconds to delay between alarm and snapshot
# in case you want to wait a second or two for the person to enter the room after triggering the sensor
DELAY = 1

#setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position for a door sensor)
GPIO.setup(SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# text message to send with photo
TXT_MSG = "Alarm Triggered!"

# directory to save the snapshot in
IMAGE_DIR = ""

# name and dimentsions of snapshot image
IMG = "gpioalarm.jpg"
IMG_WIDTH = 800
IMG_HEIGHT = 600

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'GPIO Alarm'

msg['From'] = 'henk@oegema.com'
msg['To'] = 'xxxx'
msg.preamble = 'GPIO 19 Alarm'

username = 'xxx'
password = 'yyy'

try:
# setup an indefinite loop that looks for the door sensor to be opened
while True:

GPIO.wait_for_edge(SENSOR, GPIO.RISING)
print("GPIO alarm!\n")
time.sleep(DELAY)
with picamera.PiCamera() as camera:
camera.resolution = (IMG_WIDTH, IMG_HEIGHT)
camera.capture(IMAGE_DIR + IMG)

# Open the files in binary mode..
image_path = '/home/pi/gpio_alarm/gpioalarm.jpg'
fp = open(image_path, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)

# Send the email via the Gmail server.
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(username,password)
s.sendmail('henk@oegema.com', 'xxx', msg.as_string())
s.quit()

finally:
GPIO.cleanup() # ensures a clean exit











Miscere utile dulci. (Ter leering ende vermaeck)
http://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  22.04.4 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & Jetson Nano (voor AI & ML)