Send e-mail from mutt using Python

1

I've been working with the Mutt mail client for a while and trying to implement it in my Python script but something is giving me an error. First tell you that I have it configured well and that I can send e-mails from the console with the command:

echo "Mensaje" |  mutt -s "Asunto" [email protected] 

But I'm trying to implement this in a Python script that I need to send a photo by mail and I'm not able, the code is this:

       fecha = time.strftime("%d%m%Y-%H%M%S")  # En esta variable se guarda la fecha actual y la hora para renombrar la foto guardada
       camera.capture('/home/pi/Desktop/RaspAlarm/Fotos/{}.jpg'.format(fecha))
       print("Capturando foto")
       time.sleep(2)
       print("Foto guardada")
       print("Enviando foto")
       os.system('echo "La alarma ha sido activada {}" .format(fecha) |  mutt -s "Alguien a abierto la puerta" [email protected] -a /home/pi/Desktop/RaspAlarm/Fotos/{}.jpg .format(fecha)')
       print("Foto enviada con exito")                    

The program does not give me any errors but it does not send the mail. I do not know if you can not use mutt within Python, or if there is some other way to send an email. I have seen that there are specific smtp libraries but I have seen it easier to use mutt although I do not know if it is possible.

Thanks in advance!

    
asked by Sergio Muñoz 09.05.2017 в 12:03
source

1 answer

1

You are using inadequately format . To os.system you pass a chain with the order, format you must apply it to the chain as a method. Right now what you pass to os.system is the following command literally :

'echo "La alarma ha sido activada {}" .format(fecha) |  mutt -s "Alguien a abierto la puerta" [email protected] -a /home/pi/Desktop/RaspAlarm/Fotos/{}.jpg .format(fecha)'

format is not being used as a method, it is part of the chain to be included within '' . Your code should be:

fecha = time.strftime("%d%m%Y-%H%M%S")  # En esta variable se guarda la fecha actual y la hora para renombrar la foto guardada
camera.capture('/home/pi/Desktop/RaspAlarm/Fotos/{}.jpg'.format(fecha))
print("Capturando foto")
time.sleep(2)
print("Foto guardada")
print("Enviando foto")
os.system('echo "La alarma ha sido activada {0}" |  mutt -s "Alguien a abierto la puerta" [email protected] -a /home/pi/Desktop/RaspAlarm/Fotos/{0}.jpg'.format(fecha))
print("Foto enviada con exito")

To clarify a little simplifying the example:

  • You do something like:

    >>> fecha = "12/10/2016"
    >>> c = 'echo "{}".format(fecha)'
           #^                      #^ 
    >>> print(c)
    echo "{}".format(fecha)
    
  • When it should be:

    >>> fecha = "12/10/2016"
    >>> c = 'echo "{}"'.format(fecha)
           #^        #^
    >>> print(c)
    echo "12/10/2016"
    
answered by 09.05.2017 / 15:29
source