繁体   English   中英

如何在python中发送带有继承类的电子邮件?

[英]How to send an e-mail with inheriting classes in python?

import time
class mesagerie(object):
    """mesage for e-mail"""
    def __init__(self, s="Error!"):
        global m
        localtime = time.asctime( time.localtime(time.time()) )

        mesage = localtime, m.group(0)
        try:
            print mesage
        except(),e:
            print s
class email(mesagerie):
    """e-mail"""
    def pas5(self):
        expeditor = 'allex.alexa11@gmail.com'
        receiver = 'nita_alexa11@yahoo.ro'
        username = 'root'
        password = 'skgaming'

        try:
                ob1 = smtplib.SMTP('srv1.cutesouthchat.com:9267')
                ob1.starttls()
                ob1.login(username, password)
                ob1.sendmail(expeditor, receiver, mesage)
                print "Message has been sent"
        except(),e:
                print "Message has not been sent"
                print e


x=mesagerie()
y=email()
y.pas5()

我想在第一堂课中制作一条消息,并在第二堂课中通过邮件将其发送给接收者。

我收到此错误:

ob1.sendmail(expeditor, receiver, mesage)

NameError: global name 'mesage' is not defined

类电子邮件没有继承类 mesagerie 的所有属性?

您似乎混淆了函数/方法、属性和全局变量。

每个类都有属性,这由subclases inhertied。 如果这些属性是函数,则它们被称为methods

但是,变量message根本不是类的属性。 它是mesagerie类构造函数中的一个普通局部变量。 一个函数中的局部变量不能在另一个函数中引用,正如您正在尝试做的那样。 您需要将message分配给属性。

更正的代码:

class mesagerie(object):
    """message for e-mail"""
    def __init__(self, s="Error!"):
        global m
        localtime = time.asctime( time.localtime(time.time()) )

        message = self.message = localtime, m.group(0)
        try:
            print mesasge
        except(),e:
            print s
class email(mesagerie):
    """e-mail"""
    def pas5(self):
        expeditor = 'allex.alexa11@gmail.com'
        receiver = 'nita_alexa11@yahoo.ro'
        username = 'root'
        password = 'skgaming'
        try:
            ob1 = smtplib.SMTP('srv1.cutesouthchat.com:9267')
            ob1.starttls()
            ob1.login(username, password)
            ob1.sendmail(expeditor, receiver, self.message)
            print "Message has been sent"
    except(),e:
            print "Message has not been sent"
            print e

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM