Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 815919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:45:17+00:00 2026-05-15T01:45:17+00:00

So I have been having a hard time sending email from my school’s email

  • 0

So I have been having a hard time sending email from my school’s email address. It is SSL and I could only find this code online by Matt Butcher that works with SSL:

import smtplib, socket

__version__ = "1.00"

__all__ = ['SMTPSSLException', 'SMTP_SSL']

SSMTP_PORT = 465

class SMTPSSLException(smtplib.SMTPException):
    """Base class for exceptions resulting from SSL negotiation."""

class SMTP_SSL (smtplib.SMTP):
    """This class provides SSL access to an SMTP server.
    SMTP over SSL typical listens on port 465. Unlike StartTLS, SMTP over SSL
    makes an SSL connection before doing a helo/ehlo. All transactions, then,
    are done over an encrypted channel.

    This class is a simple subclass of the smtplib.SMTP class that comes with
    Python. It overrides the connect() method to use an SSL socket, and it
    overrides the starttles() function to throw an error (you can't do 
    starttls within an SSL session).
    """
    certfile = None
    keyfile = None

    def __init__(self, host='', port=0, local_hostname=None, keyfile=None, certfile=None):
        """Initialize a new SSL SMTP object.

        If specified, `host' is the name of the remote host to which this object
        will connect. If specified, `port' specifies the port (on `host') to
        which this object will connect. `local_hostname' is the name of the
        localhost. By default, the value of socket.getfqdn() is used.

        An SMTPConnectError is raised if the SMTP host does not respond 
        correctly.

        An SMTPSSLError is raised if SSL negotiation fails.

        Warning: This object uses socket.ssl(), which does not do client-side
        verification of the server's cert.
        """
        self.certfile = certfile
        self.keyfile = keyfile
        smtplib.SMTP.__init__(self, host, port, local_hostname)

    def connect(self, host='localhost', port=0):
        """Connect to an SMTP server using SSL.

        `host' is localhost by default. Port will be set to 465 (the default
        SSL SMTP port) if no port is specified.

        If the host name ends with a colon (`:') followed by a number, 
        that suffix will be stripped off and the
        number interpreted as the port number to use. This will override the 
        `port' parameter.

        Note: This method is automatically invoked by __init__, if a host is
        specified during instantiation.
        """
        # MB: Most of this (Except for the socket connection code) is from 
        # the SMTP.connect() method. I changed only the bare minimum for the 
        # sake of compatibility.
        if not port and (host.find(':') == host.rfind(':')):
            i = host.rfind(':')
            if i >= 0:
                host, port = host[:i], host[i+1:]
                try: port = int(port)
                except ValueError:
                    raise socket.error, "nonnumeric port"
        if not port: port = SSMTP_PORT
        if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
        msg = "getaddrinfo returns an empty list"
        self.sock = None
        for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.sock = socket.socket(af, socktype, proto)
                if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
                self.sock.connect(sa)
                # MB: Make the SSL connection.
                sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
            except socket.error, msg:
                if self.debuglevel > 0: 
                    print>>stderr, 'connect fail:', (host, port)
                if self.sock:
                    self.sock.close()
                self.sock = None
                continue
            break
        if not self.sock:
            raise socket.error, msg

        # MB: Now set up fake socket and fake file classes.
        # Thanks to the design of smtplib, this is all we need to do
        # to get SSL working with all other methods.
        self.sock = smtplib.SSLFakeSocket(self.sock, sslobj)
        self.file = smtplib.SSLFakeFile(sslobj);

        (code, msg) = self.getreply()
        if self.debuglevel > 0: print>>stderr, "connect:", msg
        return (code, msg)

    def setkeyfile(self, keyfile):
        """Set the absolute path to a file containing a private key.

        This method will only be effective if it is called before connect().

        This key will be used to make the SSL connection."""
        self.keyfile = keyfile

    def setcertfile(self, certfile):
        """Set the absolute path to a file containing a x.509 certificate.

        This method will only be effective if it is called before connect().

        This certificate will be used to make the SSL connection."""
        self.certfile = certfile

    def starttls():
        """Raises an exception. 
        You cannot do StartTLS inside of an ssl session. Calling starttls() will
        return an SMTPSSLException"""
        raise SMTPSSLException, "Cannot perform StartTLS within SSL session."

And then my code:

import ssmtplib
conn = ssmtplib.SMTP_SSL('HOST')
conn.login('USERNAME','PW')
conn.ehlo()
conn.sendmail('FROM_EMAIL', 'TO_EMAIL', "MESSAGE")
conn.close()

And got this error:


/Users/Jake/Desktop/Beth's Program/ssmtplib.py:116: DeprecationWarning: socket.ssl() is deprecated.  Use ssl.wrap_socket() instead.
  sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
Traceback (most recent call last):
  File "emailer.py", line 5, in 
    conn = ssmtplib.SMTP_SSL('HOST')
  File "/Users/Jake/Desktop/Beth's Program/ssmtplib.py", line 79, in init
    smtplib.SMTP.init(self, host, port, local_hostname)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in init
    (code, msg) = self.connect(host, port)
  File "/Users/Jake/Desktop/Beth's Program/ssmtplib.py", line 131, in connect
    self.sock = smtplib.SSLFakeSocket(self.sock, sslobj)
AttributeError: 'module' object has no attribute 'SSLFakeSocket'

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:45:17+00:00Added an answer on May 15, 2026 at 1:45 am

    That code you’ve found seems to be for an older version, considering the deprecation warning. Maybe you can get by with the stdlib: There is a SMTP_SSL class as of Python 2.6, and as of at least 2.4 there is a starttls method on the plaintext SMTP class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been having a hard time to find anything that is usefull but
I have been having a hard time understanding how to use a singleton to
I have been having a really hard time debugging this error since the past
Ok, I have been having a VERY hard time figuring this out. I guess
This is probably a basic question, but I have been having a hard time
I have been having trouble pulling up a custom UIPickerView from the textfield's inputview
I've been having a hard time coming up a solution with this one. I'm
I'm new to iPhone development, and been having a hard time trying to figure
I've been having a hard time making a JNDI data source work. Following instructions
I've been having a hard time trying to append new headers to tables I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.