Have a look:
dbabic@dbabic-VirtualBox:~$ mail
No mail for dbabic
Now I fire the Demo/Customers list with below code:
dbabic@dbabic-VirtualBox:~$ mail
"/var/mail/dbabic": 1 message 1 new
>N 1 dbabic@mint Fri May 9 14:54 412/31495 Novi dnevni izvještaj
And it contains the report:
Date: Fri, 9 May 2025 14:54:33 +0800 (AWST)
--===============5304737977545444297==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Test
test
--===============5304737977545444297==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<html>
<head></head>
<body>
<p>Test,<br><br>
Test
<hr>
<i style="font-size: 11px">Test</i>
</p>
</body>
</html>
--===============5304737977545444297==
Content-Type: application/octet-stream; Name="customers_report_2025-05-09_14-54-26.642168.pdf"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="customers_report_2025-05-09_14-54-26.642168.pdf"
JVBERi0xLjUKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
.....
Here is the clean code for Customers Report:
import smtplib # for sending e-mail
from email.mime.multipart import MIMEMultipart # for composing MIME e-mail
from email.mime.text import MIMEText # for composing MIME text
from email.mime.application import MIMEApplication #novi dio modula!!!
from os.path import basename
def on_after_generate(report):
report.report_url = None
filename = report.report_filename
sender_pass = "" # password for SMTP server
sender_smtp = "localhost" # SMTP server
sender_port = 25 # SMTP port (here is SSL/TLS)
sender_user = "dbabic" # STMP sender account
email_one= "dbabic" #<--- this is just example with local user dbabic, use you own from DB, etc
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Novi dnevni izvještaj"
msg['From'] = sender_user
msg['To'] = email_one
# msg['Cc'] = user_email
email_recipients_list= email_one.split(",")
# e-mail text (plain)
mail_text = """Test
test
"""
# e-mail text (html)
mail_html = """\
<html>
<head></head>
<body>
<p>Test,<br><br>
Test
<hr>
<i style="font-size: 11px">Test</i>
</p>
</body>
</html>"""
with open(filename, 'rb') as f:
part = MIMEApplication(f.read(), Name=basename(filename))
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(mail_text, 'plain')
part2 = MIMEText(mail_html, 'html')
part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename))
# Attach parts into message container.
msg.attach(part1)
msg.attach(part2)
msg.attach(part)
# Send the message via local SMTP server.
s = smtplib.SMTP(sender_smtp, sender_port) # SMTP server params
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(sender_user, email_recipients_list , msg.as_string())
s.quit()
So after I save email attachment, it is here:
-rw------- 1 dbabic dbabic 316799 May 9 15:02 mbox
drwx------ 2 dbabic dbabic 4096 May 9 15:04 Mail
-rw------- 1 dbabic dbabic 232162 May 9 15:07 customers_report_2025-05-09_15-05-21.345450.pdf
You need to know how to set the email server where u running Jam. This is SIMPLE postfix on Ubuntu. Use
orig. code for advanced postfix authentication, ssl, etc....
Enjoy
D.