3

I've tried sending mail with python from gmail and it works fine. But the problem is when I created the Mail class with one method to whom I send specific string from my code, it can't be send.

class Mail:
   def send_mail(self, msg):
      import smtplib  

      fromaddr = '[email protected]'  
      toaddrs  = '[email protected]' 
      msg = msg + "something"

      print msg

      username = 'something'  
      password = 'something'  


      server = smtplib.SMTP('smtp.gmail.com:587')  
      server.starttls()  
      server.login(username,password)  
      server.sendmail(fromaddr, toaddrs, msg)  
      server.quit()  

This way it sends mail but the only thing in mail is "something" that I added to string, and print msg outputs the whole string plus "something". What could be the problem?

This is the whole class for now, and it's called

     mail = Mail()
     mail.send_mail(message)
6
  • What's the exception you're seeing? I get smtplib.SMTPAuthenticationError Commented Feb 7, 2012 at 9:54
  • No exception, I changed credentials here;) that's why you get it. Mail is sent but there is only "something" in mail and no 'msg' that I passed to function Commented Feb 7, 2012 at 9:56
  • I'm running it with my own gmail credentials and get it... odd. Without being able to execute it or see the rest of the class it reads right to me, have you tried concatenating some other way, such as ''.join((msg, 'something'))? Commented Feb 7, 2012 at 10:02
  • I don't need to concatenate, I just put it there for testing I only need to send that msg string, and I can't but I can print it inside function, odd isn't it Commented Feb 7, 2012 at 10:08
  • Without seeing your class, it's hard to know what's going on. How are you calling this method? Commented Feb 7, 2012 at 10:19

2 Answers 2

4

I don't know what was the problem, but I managed to send it using MIME that's already in python

So here is the code that works:

def send_mail(self, message):

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    gmailUser = '[email protected]'
    gmailPassword = 'something'
    recipient = '[email protected]'

    msg = MIMEMultipart()
    msg['From'] = gmailUser
    msg['To'] = recipient
    msg['Subject'] = "Subject"
    msg.attach(MIMEText(message))

    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()
Sign up to request clarification or add additional context in comments.

1 Comment

can you please help me if i have to send the same mail to multiple people than how will i do it
0

The problem is most likely that the content of the message seems to requite an extra line break between the addresses and the body of the message. The solution given by iblazevic is a much more readable way of doing it anyway though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.