Search code examples
pythonemailsmtpgmailsmtplib

Emails sent using python SMTP lib doesn't reach recipient


I have a problem using Python to send emails. The purpose of this script is that it should load all .PDF files from a specified folder and send the first file to the first email in a .CSV file and so on. This works fine, on my end, the emails appear in my sent-box at gmail.com containing the correct attachment.

On the other end, the recipients that are meant to recieve these emails doesn't get them. I've also checked the spam-boxes of these emails but they are no where to be found.

  • Anyone have clue why?
  • Also, if I send to my email, the mails reach my inbox.

Here is a part of the code where I think things goes wrong:

    #imports
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email import encoders

    counter = 0
    for mailTo in emailRecList:

      try:
        #server connection
        server = smtplib.SMTP('smtp.gmail.com',587)
        server.starttls()
        server.login(loginMail, loginPassword)

        #__Send email__
        server.sendmail(sendMail,loginMail,email)
        server.quit
        counter = counter + 1 
      except smtplib.SMTPException:
        print('Failed to send mail to: ' + mailTo)

Solution

  • This was actually quite embarrassing, but this is what's wrong:

    server.sendmail(sendMail,loginMail,email)
    

    it should be:

    server.sendmail(loginMail,mailTo,email)
    

    I would guess the reason it said that the mails where sent to the recipient was that it was defined as so in the:

    msg = MIMEMultipart()
    msg['To'] = mailTo