13

This is my normal code in my VPS hosting which provide python 2.4

def mail(receiver,Message):
    import smtplib
    try:
        s=smtplib.SMTP()
        s.connect("smtp.gmail.com",465)
        s.login("email@gmail.com", "password")
        s.sendmail("email@gmail.com", receiver, Message)
    except Exception,R:
            return R

but unfortunately return this message! : SMTP AUTH extension not supported by server.

in my computer which i've install python 2.7 i found the solution and it's work very good here is this code :

def mail(T,M):
    import smtplib
    try:
        s=smtplib.SMTP_SSL()
        s.connect("smtp.gmail.com",465)
        s.login("xxxxx@gmail.com","your_password")
        s.sendmail("xxxxx@gmail.com", T, M)
    except Exception,R:
            print R

But in the VPS which installed python 2.4 doesn't have SMTP_SSL() and return this message 'module' object has no attribute 'SMTP_SSL'

Also i've tried to upgrade my python in VPS but what happened is Damage the whole python that mean python not work at all.

void
  • 2,571
  • 2
  • 20
  • 35
Hamoudaq
  • 1,490
  • 4
  • 23
  • 42

2 Answers2

15

Guys thanks i've found the solution and this is the solution =)

def mail(receiver,Message):
    import smtplib
    try:
        s=smtplib.SMTP()
        s.connect("smtp.gmail.com",465)
        s.ehlo()
        s.starttls()
        s.ehlo()
        s.login("email@gmail.com", "password")
        s.sendmail("email@gmail.com", receiver, Message)
    except Exception,R:
            return R
Dhiraj Thakur
  • 716
  • 10
  • 25
Hamoudaq
  • 1,490
  • 4
  • 23
  • 42
  • 1
    You can't run ``ehlo`` or ``starttls`` before ``connection``. Besides not making any sense, it raises an exception (``SMTPServerDisconnected``). – emyller Jul 02 '13 at 18:15
  • 2
    You don't need to make the first s.ehlo() call. s.starttls() will call it for you. I confirmed this in 2.7, the 2.4 docs sound like it behaves the same way in that version. – Growth Mindset Dec 06 '13 at 06:36
  • It might be worth explicitly saying that it's the double ehlo() that appears to make this work.. – Simon Callan Jun 12 '17 at 14:22
0

Is SMTP.starttls() available? You can also do e.g.:

def mail(receiver,Message):
    import smtplib
    try:
        s=smtplib.SMTP()
        s.connect("smtp.gmail.com",587)
        s.starttls()
        s.login("email@gmail.com", "password")
        s.sendmail("email@gmail.com", receiver, Message)
    except Exception,R:
            return R
ldx
  • 3,984
  • 23
  • 28