示例#1
0
文件: mail.py 项目: RayFerr000/PLTL
def _smtp(ui):
    '''build an smtp connection and return a function to send mail'''
    local_hostname = ui.config('smtp', 'local_hostname')
    tls = ui.config('smtp', 'tls', 'none')
    # backward compatible: when tls = true, we use starttls.
    starttls = tls == 'starttls' or util.parsebool(tls)
    smtps = tls == 'smtps'
    if (starttls or smtps) and not util.safehasattr(socket, 'ssl'):
        raise util.Abort(_("can't use TLS: Python SSL support not installed"))
    mailhost = ui.config('smtp', 'host')
    if not mailhost:
        raise util.Abort(_('smtp.host not configured - cannot send mail'))
    verifycert = ui.config('smtp', 'verifycert', 'strict')
    if verifycert not in ['strict', 'loose']:
        if util.parsebool(verifycert) is not False:
            raise util.Abort(_('invalid smtp.verifycert configuration: %s')
                             % (verifycert))
        verifycert = False
    if (starttls or smtps) and verifycert:
        sslkwargs = sslutil.sslkwargs(ui, mailhost)
    else:
        sslkwargs = {}
    if smtps:
        ui.note(_('(using smtps)\n'))
        s = SMTPS(sslkwargs, local_hostname=local_hostname)
    elif starttls:
        s = STARTTLS(sslkwargs, local_hostname=local_hostname)
    else:
        s = smtplib.SMTP(local_hostname=local_hostname)
    if smtps:
        defaultport = 465
    else:
        defaultport = 25
    mailport = util.getport(ui.config('smtp', 'port', defaultport))
    ui.note(_('sending mail: smtp host %s, port %s\n') %
            (mailhost, mailport))
    s.connect(host=mailhost, port=mailport)
    if starttls:
        ui.note(_('(using starttls)\n'))
        s.ehlo()
        s.starttls()
        s.ehlo()
    if (starttls or smtps) and verifycert:
        ui.note(_('(verifying remote certificate)\n'))
        sslutil.validator(ui, mailhost)(s.sock, verifycert == 'strict')
    username = ui.config('smtp', 'username')
    password = ui.config('smtp', 'password')
    if username and not password:
        password = ui.getpass()
    if username and password:
        ui.note(_('(authenticating to mail server as %s)\n') %
                  (username))
        try:
            s.login(username, password)
        except smtplib.SMTPException, inst:
            raise util.Abort(inst)
示例#2
0
        def connect(self):
            self.sock = _create_connection((self.host, self.port))

            host = self.host
            if self.realhostport: # use CONNECT proxy
                _generic_proxytunnel(self)
                host = self.realhostport.rsplit(':', 1)[0]
            self.sock = sslutil.ssl_wrap_socket(
                self.sock, self.key_file, self.cert_file,
                **sslutil.sslkwargs(self.ui, host))
            sslutil.validator(self.ui, host)(self.sock)
示例#3
0
        def connect(self):
            self.sock = _create_connection((self.host, self.port))

            host = self.host
            if self.realhostport:  # use CONNECT proxy
                _generic_proxytunnel(self)
                host = self.realhostport.rsplit(':', 1)[0]
            self.sock = sslutil.ssl_wrap_socket(
                self.sock, self.key_file, self.cert_file,
                **sslutil.sslkwargs(self.ui, host))
            sslutil.validator(self.ui, host)(self.sock)
示例#4
0
def _smtp(ui):
    '''build an smtp connection and return a function to send mail'''
    local_hostname = ui.config('smtp', 'local_hostname')
    tls = ui.config('smtp', 'tls', 'none')
    # backward compatible: when tls = true, we use starttls.
    starttls = tls == 'starttls' or util.parsebool(tls)
    smtps = tls == 'smtps'
    if (starttls or smtps) and not util.safehasattr(socket, 'ssl'):
        raise util.Abort(_("can't use TLS: Python SSL support not installed"))
    mailhost = ui.config('smtp', 'host')
    if not mailhost:
        raise util.Abort(_('smtp.host not configured - cannot send mail'))
    verifycert = ui.config('smtp', 'verifycert', 'strict')
    if verifycert not in ['strict', 'loose']:
        if util.parsebool(verifycert) is not False:
            raise util.Abort(
                _('invalid smtp.verifycert configuration: %s') % (verifycert))
        verifycert = False
    if (starttls or smtps) and verifycert:
        sslkwargs = sslutil.sslkwargs(ui, mailhost)
    else:
        sslkwargs = {}
    if smtps:
        ui.note(_('(using smtps)\n'))
        s = SMTPS(sslkwargs, local_hostname=local_hostname)
    elif starttls:
        s = STARTTLS(sslkwargs, local_hostname=local_hostname)
    else:
        s = smtplib.SMTP(local_hostname=local_hostname)
    if smtps:
        defaultport = 465
    else:
        defaultport = 25
    mailport = util.getport(ui.config('smtp', 'port', defaultport))
    ui.note(_('sending mail: smtp host %s, port %s\n') % (mailhost, mailport))
    s.connect(host=mailhost, port=mailport)
    if starttls:
        ui.note(_('(using starttls)\n'))
        s.ehlo()
        s.starttls()
        s.ehlo()
    if (starttls or smtps) and verifycert:
        ui.note(_('(verifying remote certificate)\n'))
        sslutil.validator(ui, mailhost)(s.sock, verifycert == 'strict')
    username = ui.config('smtp', 'username')
    password = ui.config('smtp', 'password')
    if username and not password:
        password = ui.getpass()
    if username and password:
        ui.note(_('(authenticating to mail server as %s)\n') % (username))
        try:
            s.login(username, password)
        except smtplib.SMTPException, inst:
            raise util.Abort(inst)
示例#5
0
def _smtp(ui):
    """build an smtp connection and return a function to send mail"""
    local_hostname = ui.config("smtp", "local_hostname")
    tls = ui.config("smtp", "tls", "none")
    # backward compatible: when tls = true, we use starttls.
    starttls = tls == "starttls" or util.parsebool(tls)
    smtps = tls == "smtps"
    if (starttls or smtps) and not util.safehasattr(socket, "ssl"):
        raise util.Abort(_("can't use TLS: Python SSL support not installed"))
    mailhost = ui.config("smtp", "host")
    if not mailhost:
        raise util.Abort(_("smtp.host not configured - cannot send mail"))
    verifycert = ui.config("smtp", "verifycert", "strict")
    if verifycert not in ["strict", "loose"]:
        if util.parsebool(verifycert) is not False:
            raise util.Abort(_("invalid smtp.verifycert configuration: %s") % (verifycert))
        verifycert = False
    if (starttls or smtps) and verifycert:
        sslkwargs = sslutil.sslkwargs(ui, mailhost)
    else:
        # 'ui' is required by sslutil.wrapsocket() and set by sslkwargs()
        sslkwargs = {"ui": ui}
    if smtps:
        ui.note(_("(using smtps)\n"))
        s = SMTPS(sslkwargs, local_hostname=local_hostname)
    elif starttls:
        s = STARTTLS(sslkwargs, local_hostname=local_hostname)
    else:
        s = smtplib.SMTP(local_hostname=local_hostname)
    if smtps:
        defaultport = 465
    else:
        defaultport = 25
    mailport = util.getport(ui.config("smtp", "port", defaultport))
    ui.note(_("sending mail: smtp host %s, port %s\n") % (mailhost, mailport))
    s.connect(host=mailhost, port=mailport)
    if starttls:
        ui.note(_("(using starttls)\n"))
        s.ehlo()
        s.starttls()
        s.ehlo()
    if (starttls or smtps) and verifycert:
        ui.note(_("(verifying remote certificate)\n"))
        sslutil.validator(ui, mailhost)(s.sock, verifycert == "strict")
    username = ui.config("smtp", "username")
    password = ui.config("smtp", "password")
    if username and not password:
        password = ui.getpass()
    if username and password:
        ui.note(_("(authenticating to mail server as %s)\n") % (username))
        try:
            s.login(username, password)
        except smtplib.SMTPException as inst:
            raise util.Abort(inst)

    def send(sender, recipients, msg):
        try:
            return s.sendmail(sender, recipients, msg)
        except smtplib.SMTPRecipientsRefused as inst:
            recipients = [r[1] for r in inst.recipients.values()]
            raise util.Abort("\n" + "\n".join(recipients))
        except smtplib.SMTPException as inst:
            raise util.Abort(inst)

    return send