簡體   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