copy to Sent items when sending from Pyhton

Discuss your pilot or production implementation with other Zimbra admins or our engineers.
Post Reply
st3
Posts: 29
Joined: Mon Aug 05, 2019 8:17 am

copy to Sent items when sending from Pyhton

Post by st3 »

Hi, everyone!
I'm sending messages to clients from different Python and PHP scripts and the messages are not saved to the accounts Sent Items folder. I googled around and the only suggestions I found were to add a Bcc rule and then create filters that move the message to sent items and mark as read. That's not a good option form me as I have a lot of mail account that need to do this and some of them are used in scripts as well as in outlook accounts. Bcc would create duplicates for the messages that are being sent from outlook.

All the posts suggesting Bcc as an option are from 2018/19, has anything changed since? What are options? Maybe some plugins?

I use Zimbra FOSS 8.8.15

Thanx in advance! ;)
liverpoolfcfan
Elite member
Elite member
Posts: 1096
Joined: Sat Sep 13, 2014 12:47 am

Re: copy to Sent items when sending from Pyhton

Post by liverpoolfcfan »

How are you sending the emails? Are you using the SOAP API?

In the Admin tool for the user's Class Of Service->Preferences-Sending Mail - do you have "Save to sent" checked? And in the Admin tool for the account ->Preferences-> is the same box checked?
st3
Posts: 29
Joined: Mon Aug 05, 2019 8:17 am

Re: copy to Sent items when sending from Pyhton

Post by st3 »

Yes. Save to sent is enabled in both places.
I am sending by connecting to SMTP server directly like this:

Code: Select all

#!/usr/bin/python

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
def sendEmail(sender,recepient,subject,message,passwd):
        msg.set_content(message)
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = recepient
        smtpsrv = smtplib.SMTP('10.0.1.112', 587)
        smtpsrv.ehlo()
        smtpsrv.starttls()
        smtpsrv.login(sender,passwd)
        smtpsrv.send_message(msg)

sendEmail('localuser@zimbraserver.com','someone@gmail.com',"subject", "message body",'password')
jjakob
Posts: 10
Joined: Thu Jan 14, 2021 1:13 pm

Re: copy to Sent items when sending from Pyhton

Post by jjakob »

Mails sent via SMTP don't get saved in the Sent folder. You need to use IMAP or POP to save them. Or the SOAP API as already mentioned.
Some MTA's can be configured to save to IMAP the same time as they send the mail via SMTP.
st3
Posts: 29
Joined: Mon Aug 05, 2019 8:17 am

Re: copy to Sent items when sending from Pyhton

Post by st3 »

Yes, Than you! Imap works fine.
Ended up using this script for Python:

Code: Select all

import time
import ssl
import imaplib
import smtplib
import email

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class Mail:
    def __init__(self):
        # considering the same user and pass for smtp an imap
        self.mail_user = 'user@example.com'
        self.mail_pass = 'parole123'
        self.mail_host = 'zimbrapoc.example.com'


    def send_email(self, to, subject, body, path, attach):
        message = MIMEMultipart()
        message["From"] = self.mail_user
        message["To"] = to
        message["Subject"] = subject
        message.attach(MIMEText(body, "plain"))

        with open(path + attach, "rb") as attachment:
            part = MIMEBase("application", "octet-stream")
            part.set_payload(attachment.read())
        encoders.encode_base64(part)

        part.add_header(
            "Content-Disposition",
            "attachment; filename= \"" + attach + "\"",
        )
        message.attach(part)
        text = message.as_string()

        context = ssl.create_default_context()
        with smtplib.SMTP_SSL(self.mail_host, 465, context=context) as server:
            result = server.login(self.mail_user, self.mail_pass)
            server.sendmail(self.mail_user, to, text)

        imap = imaplib.IMAP4_SSL(self.mail_host, 993)
        imap.login(self.mail_user, self.mail_pass)
        imap.append('Sent', '\\Seen', imaplib.Time2Internaldate(time.time()), text.encode('utf8'))
        imap.logout()


if __name__ == '__main__':
    m = Mail()
    m.send_email('someone@gmail.com', 'Hello', 'Its just a test!', '/tmp/', 'test.pdf')
Jhon
Posts: 2
Joined: Fri Oct 28, 2022 10:24 am

Re: copy to Sent items when sending from Pyhton

Post by Jhon »

Hi, st3. Using a similar code with imap and also yours, I have a problem with SSL certificate, you know how to solve it ? The error : SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)
Post Reply