Python Forum
sending email by exchangelib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending email by exchangelib
#1
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 = ['[email protected]']

        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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending Out Email via Python JoeDainton123 1 4,762 Aug-31-2020, 12:54 AM
Last Post: nilamo
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,877 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  Sending an email with attachment without using SMTP? PythonNPC 5 3,167 May-05-2020, 07:58 AM
Last Post: PythonNPC
  sending html file in email santoshi 3 6,528 Apr-05-2019, 03:59 PM
Last Post: nilamo
  Problem in sending email using python nhoh007 1 2,523 Aug-25-2018, 07:20 PM
Last Post: typic
  An email with inline jpg cannot be read by all email clients fpiraneo 4 3,976 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  sending email rwahdan 7 8,791 Jul-14-2017, 07:28 PM
Last Post: snippsat
  Email - Send email using Windows Live Mail cyberzen 2 5,906 Apr-13-2017, 03:14 AM
Last Post: cyberzen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020