Email senden Problem [gelöst]

  • Hi Leute,

    ich Neuling hab wieder ein Problem! Ich hab schon gesucht, aber ich finde nichts.

    Wenn ich das einfach Script hier aufrufe, bekomme ich immer einen ImportError: No module named MIMEMultipart

    [code=php]import smtplib

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("YOUR EMAIL ADDRESS", "YOUR PASSWORD")

    msg = "YOUR MESSAGE!"
    server.sendmail("YOUR EMAIL ADDRESS", "THE EMAIL ADDRESS TO SEND TO", msg)
    server.quit()[/php]

    was mache ich denn falsch? was fehlt mir noch?

    Einmal editiert, zuletzt von antaril (1. Februar 2017 um 06:52)

  • [code=php]
    pi@raspberrypi:~ $ achtungtemp.py
    Traceback (most recent call last):
    File "/usr/bin/achtungtemp.py", line 4, in <module>
    import smtplib
    File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
    import email.utils
    File "/usr/bin/email.py", line 5, in <module>
    ImportError: No module named MIMEMultipart
    [/php]
    das kommt da raus als Fehler!!


  • Warum legst du deine Programme in ``/usr/bin/`` ab und wie sieht ``email.py`` aus???

    ohne Grund. Ist das schlimm? email.py ist leer seh ich grad
    Automatisch zusammengefügt:


    Versuchs doch mal mit:

    Python
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText


    Sonst ist da eine Anleitung:
    http://naelshiab.com/tutorial-send-email-python/


    leider das selbe

    Einmal editiert, zuletzt von antaril (29. Januar 2017 um 17:31)

  • Bei mir funktioniert das mit 1und1.de problemlos.

    [code=php]
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    import smtplib
    import glob
    import datetime
    from os.path import basename
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.utils import COMMASPACE, formatdate

    #Mail parameter
    smtp_server = 'smtp.1und1.de' # 1und1 SMTP Server
    smtp_port = 587
    benutzer = 'xxxxx'
    pwd = 'yyyyyy'
    sender = 'zzzz.de'
    receiver = 'aaa@bbb.de' # mehrer receiver müssen mit ', ' getrennt werden
    subject = 'Raspi-Datei WWarmwasser.png und Fussbodenheizung.png'
    preambletext = 'Graphikdateien vom Raspi02, die um 0:01 Uhr erstellt wurden, fuer den vorangegangenen Tag.'
    filepath_selected = "/media/usb/daten/*.png"

    msg = MIMEMultipart()
    msg['From'] = sender
    msg['To'] = receiver
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach(MIMEText(preambletext))

    for file in glob.glob(filepath_selected):
    with open(file, "rb") as fp:
    part = MIMEApplication(
    fp.read(),
    Name=basename(file)
    )
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(file)
    msg.attach(part)

    # Send the email via 1und1 SMTP server.
    try:
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.ehlo()
    server.starttls()
    server.ehlo ()
    server.login(benutzer, pwd)
    server.send_message(msg)
    server.quit()
    now = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
    print( now + " - Wettergraphiken erfolgreich gesendet")
    except:
    now = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')
    print(now + " - Achtung! e-mail wurde nicht versendet")

    [/php]

  • Kann das hier funktionieren wenn ich bei ner bestimmten Temperatur eine Mail an mich will??

    [code=php]
    #!/usr/bin/python

    import os
    import glob
    import time
    import smtplib
    import math
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText


    base_dir = '/sys/bus/w1/devices/'
    device_folder = [glob.glob(base_dir + '28*')[0],glob.glob(base_dir + '28*')[1]]
    device_file = [device_folder[0] + '/w1_slave', device_folder[1] + '/w1_slave']
    def sendmail():
    global mail_sent
    msg = MIMEMultipart()
    msg['From'] = 'SENDER EMAIL GOES HERE'
    msg['To'] = 'RECEIVE EMAIL GOES HERE'
    msg['Subject'] = 'TEMPERATURE ALERT'
    message = 'TEMPERATURE HAS REACHED:'+ temp_str
    msg.attach(MIMEText(message))

    mailserver = smtplib.SMTP('SMTP SERVER', SMTP PORT)
    # initial identification to smtp server
    mailserver.ehlo()
    # secure all messages with tls encryption
    mailserver.starttls()
    # re-identify with an encrypted connection
    mailserver.ehlo()
    mailserver.login('SENDER SMTP EMAIL', 'SENDER SMTP PASSWORD')

    mailserver.sendmail('SENDER','RECEIVER',msg.as_string())

    mailserver.quit()
    #Add another mail sent to counter variable
    mail_sent = 1


    def read_temp_raw(device):
    """Pass this function either 0 or 1 to get the raw data from the
    Temperature Sensor"""
    f = open(device_file[device], 'r')
    lines = f.readlines()
    f.close()
    return lines

    def read_temp(device):
    """Pass 0 or 1 to get the temperature in celcius of either sensor"""
    lines = read_temp_raw(device) #missing "device"
    while lines[0].strip()[-3:] != 'YES':
    time.sleep(0.2)
    lines = read_temp_raw(device)
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
    temp_string = lines[1][equals_pos+2:]
    temp_c = float(temp_string) / 1000.0

    while True:
    try:
    mail = sendmail

    if (temp1 < -20):
    mail
    time.sleep(30)
    # Stop on Ctrl+C and clean up GPIO pins
    except KeyboardInterrupt:
    GPIO.cleanup()
    [/php]

    Einmal editiert, zuletzt von antaril (29. Januar 2017 um 22:43)

  • Ok, hier mein Script zur automatischen Temp Überwachung und Email falls zu warm ;)

    [code=php]#!/usr/bin/python
    # -*- coding: utf-8 -*-

    #Import modules
    from datetime import datetime
    from time import sleep
    #from email.MIMEMultipart import MIMEMultipart
    import smtplib
    from email.mime.text import MIMEText
    import os
    #Clear the screen
    os.system('clear')
    #Print start testing and ideal range
    print
    print "Kuehlueberwachung"
    print "Version 0.6"
    print
    print "Überwache die Temperatur des gefrierschranks. Ideale Temperatur beträgt -10 - -19 °C."
    print
    print "Sobald die Temperatur zu niedrig oder zu hoch ist, wird ihnen eine Email zugeschickt."
    print
    print
    print

    #set values
    warningcount = 0
    max = -17
    min = -20

    #open sensor data and make into a useable string
    while True:
    tfile = open("/sys/bus/w1/devices/28-03146b546bff/w1_slave")
    text = tfile.read()
    tfile.close()
    tempdata = text.split("\n")[1].split(" ")[9]
    temp = float(tempdata[2: ])
    temp = temp / 1000
    #Print on screen information
    print "Time", datetime.time(datetime.now())
    print
    print temp, "°C."
    print "................"

    #Check temperature ranges
    if temp > max:
    print "WARNUNG!"
    print "Die Kühltruhe wird zu kalt, da stimmt evt etwas nicht!"
    print "........................."
    warningcount = warningcount+1
    print "Warning count - ", warningcount, "of 6."
    print
    print "........................."
    if temp < min:
    print "WARNUNG!"
    print "Die Kühltruhe wird zu warm! Bitte kontrollieren."
    print "........................."
    warningcount = warningcount+1
    print "Warning count - ", warningcount, "of 6."
    print
    print "........................."

    #check for warnings
    if warningcount == 6:
    #create email
    message = """Temperatur der Kuehltruhe ist ausserhalb des idealen Bereichs, sie betraegt. {} """.format(temp)
    msg = MIMEText (message)
    msg['Subject'] = 'Kühltruhentemperatur!'
    msg['From'] = 'antarilpepsticker@gmail.com>'
    msg['To'] = 'pepsticker@gmx.de'
    # send the email
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("x@gmail.com", "x")
    SUBJECT = "Hello!"
    msg = message
    server.sendmail("x@gmail.com", "x@gmx.de", msg)
    server.quit()
    warningcount = 0
    print
    print "Achtung, der Sensor hat 6 mal eine Temperatur außerhalb des idealen Bereichs gemessen!"
    print "Warnungsemail wurde gesendet!! Warnungen werden resettet."
    print "....................."
    print
    print

    #sleep for 10 minutes
    sleep(600)


    [/php]

Jetzt mitmachen!

Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!