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!
copy to Sent items when sending from Pyhton
-
- Elite member
- Posts: 1112
- Joined: Sat Sep 13, 2014 12:47 am
Re: copy to Sent items when sending from Pyhton
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?
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?
Re: copy to Sent items when sending from Pyhton
Yes. Save to sent is enabled in both places.
I am sending by connecting to SMTP server directly like this:
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')
Re: copy to Sent items when sending from Pyhton
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.
Some MTA's can be configured to save to IMAP the same time as they send the mail via SMTP.
Re: copy to Sent items when sending from Pyhton
Yes, Than you! Imap works fine.
Ended up using this script for Python:
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')
Re: copy to Sent items when sending from Pyhton
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)