sp_send_dbmail fails with attachment
Too long to comment.
Change @exclude_query_output = 0
to @exclude_query_output = 1
. This will likely print a message (error message) on what is going on.
In your example, I was able to get around the error:
Failed to initialize sqlcmd library with error number -2147024809.
that I got and I expect you'll likely get by changing @query_result_header = 0
to @query_result_header = 1
. I'm not sure why the lack of headers is causing it to fail, but it fixed it on my dev box.