Python Forum

Full Version: sending email by exchangelib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody
Have some troubles while creating a mini-app for sending payroll docs to employees. Everything works fine except one. When I press 'send payroll docs' there are eventually two whole same letters with attached docs in target-mailbox. I've racked my brain but have no idea what is wrong and why my script sends two letters instead of one.
Can anybody help me?

import sys
import tempfile
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog,QMainWindow
from PyQt5.QtWidgets import QMessageBox
from PyQt5.uic import loadUi
from exchangelib import ServiceAccount, Configuration, Account, DELEGATE, Credentials
from exchangelib import Message, Mailbox, FileAttachment
from smb.SMBConnection import SMBConnection
from nmb.NetBIOS import NetBIOS


class Mail_Send(QMainWindow):
    def __init__(self):
        super(Mail_Send, self).__init__()
        loadUi('mainwindow.ui',self)
        self.setWindowTitle('Paystubs')
        self.pushButton.clicked.connect(self.on_pushButton_clicked)
    @pyqtSlot()

    def on_pushButton_clicked(self):
        login = self.lineEdit.text()
        password = self.lineEdit_2.text()
        account = Account(login, credentials=Credentials(login, password), autodiscover=True)
        
        addressbook = ['a.konabayev@czz.kz']

        for contact in addressbook:
            attachments=[]
            with open('C:/Users/a.konabayev/Downloads/123.rtf', 'rb') as f:
                content = f.read()
            attachments.append(('123.rtf', content))
            to_recipients = [Mailbox(email_address=contact)]
    
            m = Message(account=account,
                        folder=account.sent,
                        subject='Paystub',
                        body='There is your personal paystub in attachment',
                        to_recipients=to_recipients)

            for attachment_name, attachment_content in attachments:
                file = FileAttachment(name=attachment_name, content=attachment_content)
                m.attach(file)
            m.send_and_save()
            

        
app=QApplication(sys.argv)
widget=Mail_Send()
widget.show()
sys.exit(app.exec_())[icode][/icode]