My approach to this situation was not to attach directly the pdf to the email because this require some headache (emails being saved in DB means you need to save pdf in DB which is ugly) but:
1.
Include a link in the orderConfirmationEmail like this:
Please download your invoice from <a href="http://www.myshop.com/orderdetails/pdfasguest/%Order.OrderId%">here</a>
(you do this in admin - messages template)
2.
Add a new route for this link in RouterProvider.cs in //orders section
routes.MapLocalizedRoute("GetOrderPdfInvoiceAsGuest",
"orderdetails/pdfasguest/{orderId}",
new { controller = "Order", action = "GetPdfInvoiceAsGuest" },
new[] { "Nop.Web.Controllers" });
3.
Add a method for this route in OrderController.cs
public ActionResult GetPdfInvoiceAsGuest(string orderId)
{
string orderIdDecoded = DecodeFrom64(orderId.ToString());
var order = _orderService.GetOrderById(Int32.Parse(orderIdDecoded));
if (order == null || order.Deleted)
return new HttpUnauthorizedResult();
string fileName = string.Format("order_{0}_{1}.pdf", order.OrderGuid, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
string filePath = string.Format("{0}content\\files\\ExportImport\\{1}", this.Request.PhysicalApplicationPath, fileName);
_pdfService.PrintOrderToPdf(order, _workContext.WorkingLanguage, filePath);
var pdfBytes = System.IO.File.ReadAllBytes(filePath);
return File(pdfBytes, "application/pdf", fileName);
}
- as you notice, the order id in the link is decoded from base64 because I put orderId in the link to be base64 encoded to not be plain text easy-readable by a user (of couse, you can increase the security here if you want)
4. Last step,
build Order.Id parameter for the link where rest of the parameters are built: MessageTokenProviders.cs, AddOrderTokes method:
tokens.Add(new Token("Order.OrderId", EncodeTo64(order.Id.ToString())));
That's all....
Hope to be helpfull for someone.