|
| 1 | +--- |
| 2 | +title: "Resend" |
| 3 | +description: "Learn how to generate and send invoice PDFs as email attachments using Resend" |
| 4 | +icon: "envelope" |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Learn to generate a simple invoice and send it as an email attachment using [Resend](https://resend.com). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +1. [Publish the invoice template](/usage/publishing-to-the-cloud) to the htmldocs. |
| 14 | + |
| 15 | +2. Install the required dependencies: |
| 16 | + |
| 17 | +<CodeGroup> |
| 18 | +```bash npm |
| 19 | +npm install resend |
| 20 | +``` |
| 21 | + |
| 22 | +```bash pnpm |
| 23 | +pnpm install resend |
| 24 | +``` |
| 25 | + |
| 26 | +```bash yarn |
| 27 | +yarn install resend |
| 28 | +``` |
| 29 | + |
| 30 | +```bash bun |
| 31 | +bun install resend |
| 32 | +``` |
| 33 | +</CodeGroup> |
| 34 | + |
| 35 | +3. Set up your Resend API key in your environment: |
| 36 | + |
| 37 | +```bash |
| 38 | +RESEND_API_KEY=your_resend_api_key |
| 39 | +``` |
| 40 | + |
| 41 | +## Implementation |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { Resend } from 'resend'; |
| 45 | + |
| 46 | +const resend = new Resend(process.env.RESEND_API_KEY); |
| 47 | + |
| 48 | +interface BilledTo { |
| 49 | + name: string; |
| 50 | + address: string; |
| 51 | + city: string; |
| 52 | + state: string; |
| 53 | + zip: string; |
| 54 | + phone: string; |
| 55 | +} |
| 56 | + |
| 57 | +interface YourCompany { |
| 58 | + name: string; |
| 59 | + address: string; |
| 60 | + city: string; |
| 61 | + state: string; |
| 62 | + zip: string; |
| 63 | + taxId: string; |
| 64 | + phone: string; |
| 65 | + email: string; |
| 66 | +} |
| 67 | + |
| 68 | +interface Service { |
| 69 | + name: string; |
| 70 | + description?: string; |
| 71 | + quantity: number; |
| 72 | + rate: number; |
| 73 | +} |
| 74 | + |
| 75 | +async function sendInvoiceEmail() { |
| 76 | + try { |
| 77 | + // 1. Generate the invoice using htmldocs API |
| 78 | + const response = await fetch('https://api.htmldocs.com/api/documents/invoice', { |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'Authorization': `Bearer ${process.env.HTMLDOCS_API_KEY}`, |
| 82 | + 'Content-Type': 'application/json', |
| 83 | + }, |
| 84 | + body: JSON.stringify({ |
| 85 | + format: 'pdf', |
| 86 | + props: { |
| 87 | + billedTo: { |
| 88 | + name: "Acme Corp", |
| 89 | + address: "123 Business Ave", |
| 90 | + city: "San Francisco", |
| 91 | + state: "CA", |
| 92 | + zip: "94107", |
| 93 | + phone: "555-0123" |
| 94 | + }, |
| 95 | + yourCompany: { |
| 96 | + name: "Your Company", |
| 97 | + address: "456 Banana Rd.", |
| 98 | + city: "San Francisco", |
| 99 | + state: "CA", |
| 100 | + zip: "94107", |
| 101 | + taxId: "00XXXXX1234X0XX", |
| 102 | + phone: "123-456-7890", |
| 103 | + email: "hello@email.com" |
| 104 | + }, |
| 105 | + services: [ |
| 106 | + { |
| 107 | + name: "Premium License", |
| 108 | + description: "Annual subscription", |
| 109 | + quantity: 1, |
| 110 | + rate: 999.00 |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | + }) |
| 115 | + }); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + const errorData = await response.json().catch(() => null); |
| 119 | + throw new Error( |
| 120 | + `Failed to generate invoice: ${response.status} ${response.statusText}${ |
| 121 | + errorData ? ` - ${JSON.stringify(errorData)}` : '' |
| 122 | + }` |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + const documentBuffer = await response.arrayBuffer(); |
| 127 | + |
| 128 | + // 2. Send email with the generated invoice |
| 129 | + const data = await resend.emails.send({ |
| 130 | + from: 'you@yourdomain.com', |
| 131 | + to: 'recipient@example.com', |
| 132 | + subject: 'Invoice for Your Recent Services', |
| 133 | + attachments: [ |
| 134 | + { |
| 135 | + content: Buffer.from(documentBuffer), |
| 136 | + filename: 'invoice.pdf', |
| 137 | + }, |
| 138 | + ], |
| 139 | + html: ` |
| 140 | + <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto;"> |
| 141 | + <h2 style="color: #1a1a1a;">Your Invoice</h2> |
| 142 | + |
| 143 | + <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> |
| 144 | + Thank you for your business. Please find your invoice attached to this email. |
| 145 | + </p> |
| 146 | + |
| 147 | + <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> |
| 148 | + Payment is due within 15 days from the invoice date. For your convenience, |
| 149 | + payment instructions are included in the invoice. |
| 150 | + </p> |
| 151 | + |
| 152 | + <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> |
| 153 | + If you have any questions about this invoice, please contact our billing department |
| 154 | + using the contact information provided in the invoice. |
| 155 | + </p> |
| 156 | + |
| 157 | + <hr style="border: none; border-top: 1px solid #e1e1e1; margin: 30px 0;" /> |
| 158 | + |
| 159 | + <p style="color: #898989; font-size: 14px;"> |
| 160 | + This is an automated message. Please do not reply to this email. |
| 161 | + </p> |
| 162 | + </div> |
| 163 | + `, |
| 164 | + }); |
| 165 | + |
| 166 | + console.log('Email sent successfully:', data); |
| 167 | + } catch (error) { |
| 168 | + if (error instanceof Error) { |
| 169 | + console.error('Error:', error.message); |
| 170 | + } else { |
| 171 | + console.error('Unknown error:', error); |
| 172 | + } |
| 173 | + throw error; // Re-throw to handle it in the calling code |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +For more information about document generation, see our [API Reference](/api-reference/generate-document). |
0 commit comments