ERPNext Conference 2019* ERPNext.com Blog

V12 error on sending email with document pdf


#1

Dear colleagues,

I have the following error in an ERPNext V12 instance: when trying to send an email from a document (e.g. supplier quotation) with the document print attached, the following error is returned:

'OSError' object has no attribute 'message'
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/pdfkit/configuration.py", line 21, in __init__
    with open(self.wkhtmltopdf) as f:
FileNotFoundError: [Errno 2] No such file or directory: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/pdf.py", line 25, in get_pdf
    filedata = pdfkit.from_string(html, False, options=options or {})
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/pdfkit/api.py", line 70, in from_string
    configuration=configuration, cover_first=cover_first)
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/pdfkit/pdfkit.py", line 42, in __init__
    self.configuration = (Configuration() if configuration is None
  File "/home/frappe/frappe-bench/env/lib/python3.5/site-packages/pdfkit/configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/queue.py", line 404, in send_one
    message = prepare_message(email, recipient.recipient, recipients_list)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/queue.py", line 536, in prepare_message
    print_format_file = frappe.attach_print(**attachment)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1391, in attach_print
    "fcontent": get_print(doctype, name, print_format=print_format, style=style, html=html, as_pdf=True, doc=doc, no_letterhead=no_letterhead, password=password)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1368, in get_print
    return get_pdf(html, output = output, options = options)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/pdf.py", line 32, in get_pdf
    if ("ContentNotFoundError" in e.message
AttributeError: 'OSError' object has no attribute 'message'

The email can be sent without the attachement without any issue. Also, the PDF renders without any issues from the print preview > PDF. And, if the mail is sent manually from the Email Queue using the send button while it is “not sent”, it works as well.

Any ideas what could go wrong?


Email messages hanging in the queue for a long time
#2

Possibly a wkhtmltopdf version mismatch, but otherwise a full reinstall of latest version might help…

sudo apt install -y xfonts-75dpi fontconfig libxrender1 xfonts-base
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb
sudo dpkg --install wkhtmltox_0.12.5-1.stretch_amd64.deb


#3

Thanks, @trentmu. I found wkhtmltopdf working. The error stricly only occurs when the mail is picked up from the crontab job running

$ cd /home/frappe/frappe-bench && /home/frappe/frappe-bench/env/bin/bench execute frappe.email.queue.flush

This is the case as the automatic sending is broken after v11 upgrade (see also V11 can’t suspend nor resume email queue). While the resume button is back on in v12, it still has no impact… If the exact same mail is sent by pushing “Send now”, it works…


#4

Is there anything in cron log? Maybe restart the cron service


#5

I don’t have exactly the answer, but this has somehow to do with the wkhtmltopdf not being found. Cron will create a shell for the job, so make sure the executable is in one of your search path.

$ whereis wkhtmltopdf
$ echo $PATH

Try running your command in a bash shell
cd /home/frappe/frappe-bench && /bin/bash /home/frappe/frappe-bench/env/bin/bench execute frappe.email.queue.flush


#6

Thanks for your input.

Interestingly, executing the command from the command shell always works without an issue (mail is sent with pdf attachement). In the cron-job (tested both root and frappe) it fails with the above error message.

Tested the location:

$ which wkhtmltopdf
/usr/local/bin/wkhtmltopdf
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

That should be fine…

The cron log shows that it was executed:

$ sudo grep CRON /var/log/syslog
Sep  9 10:45:01 lc-****-01 CRON[2675]: (frappe) CMD (cd /home/frappe/frappe-bench && /usr/local/bin/bench execute frappe.email.queue.flush)

It seems like the process triggered from cron does not have sufficient access rights to run wkhtmltopdf…


#7

Perhaps try creating a bash shell script with the commands, and let the cron call your script to see it it makes a difference. (Disclaimer: just guessing here.)


#8

Is your host ip set in site_config.json ? ( another guess)


#9

Hi @smino,

thanks for your input. The hostname is set in the site_config.json, yes…


#10

Resolved by installing a new VM in V12 with the email scheduler active and restored the backup. Rather frustrating but works.


#11

Actually, the error was back again as soon as the system was fully restored. Email queue hung again.

However, with this workaround it seems to be properly resolved:

$ sudo crontab -e
* * * * * cd /home/frappe/frappe-bench && sudo -u frappe /usr/local/bin/bench trigger-scheduler-event all

Note that the cron job has to be executed as user frappe (sudo -u frappe), otherwise the initial error with the pdf creation occurs again…