This topic was automatically generated from Slack. You can find the original thread here.
I have a specific use case that I would like to solve. I need to convert an email body to PDF, so that’s my idea:
• I send this email to a monitored email, like convert-to-pdf@mydomain.com, CCing the final recipient
• When this email arrives at this monitored inbox, a process converts the email body to PDF and sends it, via email attachment, to the final recipient
Would that work using Pipedream?
Yes, you can accomplish this use case with Pipedream! Here’s how you can set it up step-by-step: — ## Workflow Overview 1. Trigger: Use the Email trigger in Pipedream. This gives you a unique email address (like [convert-to-pdf@pipedream.net](mailto:convert-to-pdf@pipedream.net)). Any email sent here will trigger your workflow. 2. Extract Email Body: Access the email body from the trigger event. 3. Convert to PDF: Use a code step (Node.js or Python) with a library like pdfkit (Node.js) or fpdf (Python) to generate a PDF from the email body. 4. Send PDF as Attachment: Use the Email action to send the PDF as an attachment to the intended recipient. — ## Step-by-Step Instructions ### 1. Set up the Email Trigger - In your Pipedream workflow, add the Email trigger. - Pipedream will give you a unique email address. Send emails to this address (and CC the final recipient). ### 2. Extract the Email Body The email body will be available at steps.trigger.event.text or steps.trigger.event.html. ### 3. Convert Email Body to PDF Add a Node.js code step like this: import fs from "fs"; import PDFDocument from "pdfkit"; export default defineComponent({ async run({ steps, $ }) { const doc = new PDFDocument(); const filePath = "/tmp/email.pdf"; const stream = fs.createWriteStream(filePath); doc.pipe(stream); doc.text(steps.trigger.event.text || "No body found"); doc.end(); // Wait for the PDF to be written await new Promise((resolve) => stream.on("finish", resolve)); return filePath; }, }); ### 4. Send the PDF as an Email Attachment Add an Email action step to send the PDF to the recipient. Example: export default defineComponent({ props: { email: { type: "app", app: "email", }, }, async run({ steps, $ }) { const recipient = [steps.trigger.event.cc[0]](http://steps.trigger.event.cc[0]) || "[recipient@example.com](mailto:recipient@example.com)"; // Use CC as recipient $.send.email({ to: recipient, subject: "Converted PDF", text: "Please find the PDF attached.", attachments: [ { path: steps.code.$return_value, // Path to the PDF file filename: "email.pdf", }, ], }); }, }); — ## Notes - You may need to adjust the recipient logic depending on how you CC the final recipient. - If you want to convert HTML emails to PDF, consider using a library like puppeteer for better formatting. - All files must be written to /tmp in Pipedream. — ## References - Pipedream Email Integration Docs - Working with Files in Pipedream — In summary: Yes, your workflow is possible with Pipedream. Set up an Email trigger, convert the body to PDF, and send it as an attachment to the recipient. If you need help with the code or setup, let me know!
I am wondering if there is a better way to forward the email to the final recipient, instead of CCing them on the original email. And I had an idea. All the emails for this use case will have in their body something like “Receipt #1752-1773”. Is there a way I can find this number (“1752-1773” in this case) in the email body? With that I can add a step to search for it in my database and obtain the final recipient email address and then send it to them.
Absolutely, you can extract the “Receipt #1752-1773” number from the email body in a Pipedream workflow, use it to look up the recipient in your database, and then forward the email (or PDF) to the correct address. Here’s how you can do it step-by-step: — ## 1. Extract the Receipt Number from the Email Body Add a Node.js code step after your email trigger to extract the receipt number using a regular expression: export default defineComponent({ async run({ steps, $ }) { // Get the email body (plain text or HTML) const body = steps.trigger.event.text || steps.trigger.event.html || ""; // Regex to match "Receipt #1752-1773" const match = body.match(/Receipt\s+#([0-9\-]+)/i); if (!match) { throw new Error("Receipt number not found in email body"); } const receiptNumber = match[1]; $.export("receiptNumber", receiptNumber); return receiptNumber; } }); — ## 2. Query Your Database for the Recipient Email Add a step to query your database using the extracted receipt number. If you use the Database app, you can use the “Query SQL Database” action, or write a custom code step: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { database: { type: "app", app: "database", }, receiptNumber: { type: "string", label: "Receipt Number", }, }, async run({ steps, $ }) { // Example for a SQL database (adjust table/column names as needed) const sql = `SELECT email FROM receipts WHERE receipt_number = '${this.receiptNumber}' LIMIT 1;`; // Use your database app's query method or axios if needed // This is a generic example, adapt to your DB and connection method const result = await axios($, { // Replace with your DB query endpoint/config // url: "...", // method: "...", // data: { query: sql }, }); // For Pipedream's Database app, use the built-in Query SQL Database action instead // and pass the receipt number as a param // Return the recipient email // return result.data[0].email; return result; } }); Or, use the Query SQL Database action directly in the UI, passing the receipt number from the previous step. — ## 3. Send the Email to the Final Recipient Now, add an Email action step and use the recipient email returned from your database lookup. You can use the Send Yourself an Email action or a custom code step: ```
export default defineComponent({ props: { email: { type: “app”, app: “email”, }, recipient: { type: “string”, label: “Recipient Email”, }, subject: { type: “string”, label: “Subject”, }, body: { type: “string”, label: “Body”, }, }, async run({ steps, $ }) { $.send.email({ to: this.recipient, subject: this.subject, text: this.body, // Add attachments if needed }); } });