-1

I want to send a trial email from Google, but I couldn't solve this error. There's something wrong with "mail.sendmail(messsage["From"], message["To"], message.as_string())" right there. However, none of the methods I tried worked. I looked at a few examples of this, but because the samples were a little different, I opened a new topic. I will be grateful to you for your help

class SendMail:
    def __init__(self, email, auth):
        self.email = email
        self.auth = auth
        self.send()
        
    def send(self):
        try:
            mail = smtplib.SMTP("smtp.gmail.com", 587)
            mail.ehlo()
            mail.starttls()
            mail.login("example@gmail.com", "password") 

            message = MIMEMultipart()
            message["Form"] = self.email
            message["Subject"] = "Example Subject"
        
            body = f"Test message : {self.auth}"
            
            body_text = MIMEText(body, "plain")
            message.attach(body_text)
            mail.sendmail(messsage["From"], message["To"], message.as_string())
            print("message was sent successfully..")
            mail.close()
        except Exception as e:
            print(e)

Exactly the error message : 'NoneType' object has no attribute 'strip'

  • can you please paste the entire traceback? – Jeril Jan 27 '22 at 09:59
  • Presumably `message["Form"]` should be `message["From"]`? – Kemp Jan 27 '22 at 10:01
  • If I change "From" to "Form", I get this error ; TypeError: 'NoneType' object is not iterable – Elvent YILDIRAN Jan 27 '22 at 10:14
  • And Traceback ; ```Traceback (most recent call last): self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist)) File "C:\Users\bilgi\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 154, in quoteaddr if addrstring.strip().startswith('<'): AttributeError: 'NoneType' object has no attribute 'strip' ``` – Elvent YILDIRAN Jan 27 '22 at 10:15
  • You understood my point backwards. You have `message["Form"] = self.email` in your code. This should be `message["From"] = self.email`. When you pass `messsage["From"]` to `mail.sendmail` you aren't actually giving it a value because you never set it. Also, please don't post things like tracebacks as comments, edit it into the post where it should have been. The lack of line breaks and such in comments makes it very hard to read. – Kemp Jan 27 '22 at 10:17
  • As an aside, your code seems to be written for Python 3.5 or earlier. The `email` library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the [examples from the `email` documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Jan 27 '22 at 10:30
  • You want to [edit] the question to include the traceback there. Comments can be deleted at any time. – tripleee Jan 27 '22 at 10:31
  • Thank you for your help, I'm learning and I'll pay a little more attention. – Elvent YILDIRAN Jan 27 '22 at 10:38

1 Answers1

0

You have the line

message["Form"] = self.email

in your code. This should be

message["From"] = self.email

Note the corrected spelling of "From". In your original code when you pass messsage["From"] to mail.sendmail you aren't actually giving it a value because you never set it. This then leads to the error being raised when it tries to manipulate the value.

Additionally, you don't appear to be setting message["To"] anywhere, but again you are passing the unset value to mail.sendmail.

Kemp
  • 3,467
  • 1
  • 18
  • 27