尝试使用SMTP发送编码的电子邮件

时间:2018-05-06 16:32:13

标签: python-3.x base64 pyqt5 smtplib

我尝试过一个程序,允许您发送带编码的电子邮件。 因此,该程序首先要求您的电子邮件地址您要发送邮件的电子邮件密码和电子邮件地址

def get_email(self):
    self.email, ok1 = QInputDialog.getText(self,"Email","Enter your e-mail adress")
    self.passw, ok2 = QInputDialog.getText(self,"Password","Enter your e-mail password")
    self.email_to,ok3 = QInputDialog.getText(self,"Sending To","Enter the email adress that you want to send e-mail")
    if ok1 and ok2 and ok3:
        print(self.email,self.passw,self.email_to)

然后,此处此功能用于向某人发送电子邮件,但必须有错误,因为我无法发送任何电子邮件:

def email_sender(self):
    content = self.encryption()
    mesaj = MIMEMultipart()

    mesaj_govdesi = MIMEText(str(content), "plain")

    mesaj.attach(mesaj_govdesi)

    mesaj["Subject"] = self.subject_text.text()  #LineEdit

    try:
        mail = smtplib.SMTP("smtp.gmail.com", 587)

        mail.ehlo()
        mail.starttls()

        mail.login(self.email, self.passw)  # e-mail adress and it's password
        mail.sendmail(self.email,self.email_to, mesaj.as_string())
        print("Mail Başarıyla Gönderildi....")
        mail.quit()
    except:
        sys.stderr.write("An error occured  ")
        sys.stderr.flush()

如果你想看看这是我用PyQt5和Base64写的整个代码:

import sys,os,base64,smtplib
from PyQt5.QtWidgets import QWidget,QApplication,QTextEdit,QLabel,QPushButton,QVBoxLayout,QFileDialog,QHBoxLayout,QInputDialog,QLineEdit
from PyQt5.QtWidgets import QAction,qApp,QMainWindow
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class Notepad(QWidget):
    def __init__(self):

        super().__init__()

        self.init_ui()
    def init_ui(self):
        self.get_email()
        self.encode_text = QTextEdit()
        self.decode_text = QTextEdit()
        self.subject_text = QLineEdit()

        self.temizle = QPushButton("Temizle")
        self.ac = QPushButton("Aç")
        self.kaydet = QPushButton("Kaydet")
        self.send_it = QPushButton("Encode and Send")
        self.decode_it = QPushButton("Decode")
        self.email_text = QLabel()
        self.encode_label = QLabel("Here Enter your message to send")
        self.decode_label = QLabel("Enter your encoded e-mail message!")
        self.subject_label = QLabel("Subject:")
        h_box = QHBoxLayout()
        h_box.addWidget(self.ac)
        h_box.addWidget(self.kaydet)
        h_box.addWidget(self.email_text)

        v_box = QVBoxLayout()
        v_box.addWidget(self.subject_label)
        v_box.addWidget(self.subject_text)
        v_box.addWidget(self.encode_label)
        v_box.addWidget(self.encode_text)
        h_box.addWidget(self.temizle)
        v_box.addWidget(self.send_it)
        v_box.addWidget(self.decode_label)
        v_box.addWidget(self.decode_text)
        v_box.addWidget(self.decode_it)

        v_box.addLayout(h_box)

        self.setLayout(v_box)

        self.setWindowTitle("Mail Cryptor 1.0")
        self.temizle.clicked.connect(self.yaziyi_temizle)
        self.ac.clicked.connect(self.dosya_ac)
        self.kaydet.clicked.connect(self.dosya_kaydet)
        self.send_it.clicked.connect(self.email_sender)
        self.decode_it.clicked.connect(self.decryption)

    def yaziyi_temizle(self):
        self.encode_text.clear()
        self.decode_text.clear()

    def dosya_ac(self):
        dosya_ismi = QFileDialog.getOpenFileName(self,"Dosya Aç",os.getenv("HOME"))

        with open(dosya_ismi[0],"r") as file:
            self.yazi_alani.setText(file.read())

    def dosya_kaydet(self):
        dosya_ismi = QFileDialog.getSaveFileName(self,"Dosya Kaydet",os.getenv("HOME"))

        with open(dosya_ismi[0],"w") as file:

            file.write(self.encode_text.toPlainText())

    def encryption(self):
        encoded = base64.b64encode(bytes(self.encode_text.toPlainText(), "utf-8"))
        print(encoded)
        return encoded

    def decryption(self):
        encoded = self.decode_text.toPlainText()
        b = base64.b64decode(encoded).decode("utf-8", "ignore")
        print(b)

    def email_sender(self):
        content = self.encryption()
        mesaj = MIMEMultipart()

        mesaj_govdesi = MIMEText(str(content), "plain")

        mesaj.attach(mesaj_govdesi)

        mesaj["Subject"] = self.subject_text.text()  #LineEdit

        try:
            mail = smtplib.SMTP("smtp.gmail.com", 587)

            mail.ehlo()
            mail.starttls()

            mail.login(self.email, self.passw)  # e-mail adress and it's password
            mail.sendmail(self.email,self.email_to, mesaj.as_string())
            print("Mail Başarıyla Gönderildi....")
            mail.close()
        except:
            sys.stderr.write("Bir sorun oluştu!")
            sys.stderr.flush()

    def get_email(self):
        self.email, ok1 = QInputDialog.getText(self,"Email","Enter your e-mail adress")
        self.passw, ok2 = QInputDialog.getText(self,"Password","Enter your e-mail password")
        self.email_to,ok3 = QInputDialog.getText(self,"Sending To","Enter the email adress that you want to send e-mail")
        print(self.email,self.passw,self.email_to)

class Menu(QMainWindow):

    def __init__(self):

        super().__init__()


        self.pencere = Notepad()

        self.setCentralWidget(self.pencere)


        self.menuleri_olustur()
    def menuleri_olustur(self):

        menubar = self.menuBar()

        dosya = menubar.addMenu("Dosya")

        dosya_ac = QAction("Dosya Aç",self)
        dosya_ac.setShortcut("Ctrl+O")

        dosya_kaydet = QAction("Dosya Kaydet",self)

        dosya_kaydet.setShortcut("Ctrl+S")

        temizle = QAction("Dosyayı Temizle",self)
        temizle.setShortcut("Ctrl+D")

        cikis = QAction("Çıkış",self)

        cikis.setShortcut("Ctrl+Q")

        dosya.addAction(dosya_ac)
        dosya.addAction(dosya_kaydet)
        dosya.addAction(temizle)
        dosya.addAction(cikis)

        dosya.triggered.connect(self.response)


        self.setWindowTitle("Mail Cryptor 1.0")

        self.show()

    def response(self,action):

        if action.text() == "Dosya Aç":
            self.pencere.dosya_ac()

        elif action.text() == "Dosya Kaydet":
            self.pencere.dosya_kaydet()
        elif action.text() == "Dosyayı Temizle":
            self.pencere.yaziyi_temizle()

        elif action.text() == "Çıkış":
            qApp.quit()








app = QApplication(sys.argv)

menu = Menu()


sys.exit(app.exec_())      

0 个答案:

没有答案