1 year ago

#375851

test-img

Amit

How to solve smtplib.SMTPDataError: (554, b'5.5.1 Error: no valid recipients') in python

I have to functions send_output_mail which will call process_mail to send mail to the given recipients with multiple attacthments.

Here is my code

import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
import pandas as pd

server = '112.11.1.111'; port = 11
MY_ADDRESS = 'xyz@outlook.com'
from datetime import datetime



def process_email(**kwargs):  # accept variable number of keyworded arguments

    # get total email recipients
    rcpt = []
    for email in kwargs['emails']:
        for i in email:
            rcpt.append(i)

    # set up the SMTP server
    s = smtplib.SMTP(host=server, port=port)

    msg = MIMEMultipart()  # create a message
    # setup the parameters of the message, to cc emails
    msg['From'] = MY_ADDRESS
    msg['To'] = ','.join(kwargs['emails'][0])
    msg['Cc'] = ','.join(kwargs['emails'][1])
    msg['Subject'] = kwargs['subject']

    if 'attachments' in kwargs.keys():
        for attachment in kwargs['attachments']:
            fullpath_attactment = os.path.join(os.getcwd(),"Output_file",attachment) #will give full path of the file 
            with open(fullpath_attactment) as fp:
                record = MIMEBase('application', 'octet-stream')
                record.set_payload(fp.read())
                encoders.encode_base64(record)
                record.add_header('Content-Disposition', 'attachment',
                                  filename=os.path.basename(fullpath_attactment))
                msg.attach(record)

    s.sendmail(MY_ADDRESS, rcpt, msg.as_string())  **#Getting Error Here**
    s.quit()


def send_output_mail():
        emails = [["xyz@outlook.com", "hy@outlook.com"], ["xxx@outlook.com","yy@outlook.com"]]
        subject = "Reports"

        process_email(emails=emails, subject=subject, attachments= ["xx.csv","yy.csv"])

Problem As i Debugged i am getting smtplib.SMTPDataError: (554, b'5.5.1 Error: no valid recipients') while executing line

 s.sendmail(MY_ADDRESS, rcpt, msg.as_string())

I have crosschecked and the mailid that i have written and it was also correct, still getting this error.

python

smtplib

outlook-restapi

0 Answers

Your Answer

Accepted video resources