0

My wife has a database of client emails for her work. Because I am studying Python, she asked me to make a program to send automatically the newsletter with the program.

I followed the code of this website :

Website where I took the code

I made many tests with my mail, her mail, a list of friend's mails (they knew that I will test this), everything worked, with different clients (google, outlook, and so on ...).

Today was the big day to launch the script to send a merry Christmas to her clients. But, for some reasons I ignore, I have this error after a few mails:

Traceback (most recent call last):
  File "C:\Users\???\Documents\Newsletter\eMailing.py", line 364, in <module>
    server.sendmail(email_address, email_receiver, message.as_string())
  File "C:\Users\???\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 903, in sendmail
    raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (550, b'Message was not accepted -- invalid mailbox.  Local mailbox client@address.com is unavailable: Invalid mPOP user address')

I censored the mail address. Is it possible to skip the error, and continue the program?

The code is in a for loop:

email_list = cleaned_email_list
counter_mails = 0

for mail in email_list:
  email_receiver = mail

  # Creation of eMail
  message = MIMEMultipart("alternative")
  # Subject
  message["Subject"] = "Музыка для твоего вдохновения"
  # From
  message["From"] = email_address
  # To
  message["To"] = email_receiver

  # Creation of the text of the mail in raw
  text = '''
  
  '''
  # Ceation of the text with HTML
  html = '''


  <html>

I deleted the code in HTML because it's too long but there is a mail in HTML here

  </html>
  '''

  # Creation of elements MIMEText
  texte_mime = MIMEText(texte, 'plain')
  html_mime = MIMEText(html, 'html')

  # Gluing elements
  message.attach(texte_mime)
  message.attach(html_mime)

  # creation of the connexion
  context = ssl.create_default_context()
  with smtplib.SMTP_SSL(smtp_address, smtp_port, context=context) as server:
    # connexion to the account
    server.login(email_address, email_password)
    # sending the mail
    server.sendmail(email_address, email_receiver, message.as_string())


    print("Mail sent to :", mail)
    counter_mails += 1

print('\n')
print('Total mails sent : ', counter_mails)

I tried to find a solution, looks like it exist a solution with the function try, but I didn't get how it works.

I would like this: when I can't send the mail, instead of the error, it jumps to the next step, doesn't execute this:

    print("Mail sent to :", mail)
    counter_mails += 1

and goes back to the " for mail in email_list: " and continues until the end of the list.

There is an answer here: The answer

The problem, I didn't understand where to put the code:

except SMTPResponseException as e:
    error_code = e.smtp_code
    error_message = e.smtp_error

Do I need to put this too?

try:
  smtpObj = smtplib.SMTP('smtp.gmail.com',587)
  smtpObj.starttls()
  smtpObj.login(sender,'foo@bar.com')
  smtpObj.sendmail(sender, receivers, message)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Alkanium
  • 17
  • 3
  • The site you found demonstrates `email` code written for an older Python version. The `email` module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) new `EmailMessage` API. Probably throw away this code and start over with modern code from [the Python `email` examples documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Dec 23 '22 at 18:18
  • I don't understand how works the modern code – Alkanium Dec 23 '22 at 18:42
  • It's mainly simpler; you don't have to explicitly build the MIME structure yourself. But what you have will still work for the time being; it's just clumsier and more work to maintain. – tripleee Dec 23 '22 at 19:15
  • Yeah I get it. The problem is, it doesn't solve my problem. Even with the new version of the code, I still have the error of smtplib – Alkanium Dec 23 '22 at 20:21
  • The duplicate shows how to use `try`/`except` – tripleee Dec 23 '22 at 20:23
  • I tried the try/except and I have an error anyway : Traceback (most recent call last): File "C:\Users\...\Documents\Newsletter\eMailing.py", line 366, in server.sendmail(email_address, email_receiver, message.as_string()) File "C:\Users\...\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 903, in sendmail raise SMTPDataError(code, resp) smtplib.SMTPDataError: (550, b'spam message rejected) Process finished with exit code 1 – Alkanium Dec 23 '22 at 20:41
  • It doesn't skip the error and go through it. I don't get why when I use my address or the addresses of my friends, we all receive the mail, and when I use the dataset of my wife, everything is locked. – Alkanium Dec 23 '22 at 20:46
  • Did you put `except SMTPDataError:`? If you still can't figure it out, perhaps post a new question with your actual code (probably just the `smtplib` part; see also the guidance for providing a [mre]). – tripleee Dec 24 '22 at 08:50
  • Having a server reject your message as spam suggests that you have been sending the same message too many times, or send a really generic message which resembles test messages or malformed messages from abusive senders. Probably stop doing that before you dig yourself into an even deeper hole. – tripleee Dec 24 '22 at 08:51
  • I contacted the support of the mail address, they said that no problem of spams was detected. And when I test to send to my own mail address, I don't have the error. The message comes from the receiver, not from the adress I use to send mails. – Alkanium Dec 24 '22 at 10:22
  • I'm not sure if that's supposed to clarify anything. Of course it's the receiving server which is rejecting the message as spam. – tripleee Dec 24 '22 at 12:26
  • Can you indicate me where I need to put exactly the code try/except ? Because I already tested any features it could be possible and I still have the same problem. Does I need to put after the server.sendmail(email_address, email_receiver, message.as_string()) or before ? – Alkanium Dec 24 '22 at 21:13
  • Again, ask a new question with your failed attempt so we can see what's wrong and how to fix it. The basic syntax of `try`/`except` is not hard to grasp; it needs the wrap the calls which could produce an error. And as mentioned, the duplicate already demonstrates it in practice. If you can't apply what's there, chances are another example won't help much either. – tripleee Dec 24 '22 at 23:02

0 Answers0