Send a CSV attachment with SendGrid

How to attach a CSV or TSV file to a SendGrid email: what base64 encoding changes, how the file holds up in spreadsheets, and when plain text in the body is the better choice.

Attaching a CSV file to an email sent through SendGrid is a common way to deliver reports, exports and logs. The process involves two transformations: the CSV itself, which turns tabular data into delimited text, and the attachment encoding, which turns that file into a string that can travel inside an API request. This review covers what each step changes, where the data can lose meaning or usability, and when a different approach serves recipients better.

Is a CSV attachment the right choice?

A CSV attachment is a sound choice when recipients need to work with the data rather than just read it, for example by sorting, filtering or importing it into another system. The main benefit is portability. CSV is plain text that almost every spreadsheet, database and scripting tool can read. The key tradeoff is that CSV carries no type information, no formatting and no declared character encoding. How the file looks when it is opened depends on the recipient's software and locale settings, not on the sender.

Plain text in the email body is usually the better option in these cases:

  • The data is only a few rows or a single summary figure that recipients will read but not process.
  • Recipients mostly read email on phones, where opening and scrolling a spreadsheet attachment is awkward.
  • The content is sensitive. An attachment is copied into every recipient's mailbox and can be forwarded without any further control. A link to an access-controlled download may fit better.
  • The file is large enough to approach SendGrid's message size limit. Check the current documented limit before relying on attachments for large exports.

What the attachment process actually changes

SendGrid's v3 Mail Send API accepts attachments as an array of objects. Each object carries the file content as a base64-encoded string, plus a filename and, optionally, a MIME type and a disposition. Base64 is a transport encoding only. It turns the file's bytes into ASCII characters that are safe inside JSON, and the recipient's mail client decodes them back to the original bytes. If you encode the raw file bytes correctly, the delivered CSV is byte-for-byte identical to the one you sent.

The risks therefore come from how the CSV is produced and how it is read, not from SendGrid's transport. A minimal attachment object in the request body looks like this:

"attachments": [
  {
    "content": "<base64 of the CSV file bytes>",
    "filename": "report.csv",
    "type": "text/csv",
    "disposition": "attachment"
  }
]

Using SendGrid's Node.js library, reading a file from disk and sending it looks like the example below. The API key, recipient and sender are read from environment variables. The sender must be one you have verified in your SendGrid account.

const sgMail = require('@sendgrid/mail');
const fs = require('fs');

sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const csvBytes = fs.readFileSync('report.csv');

await sgMail.send({
  to: process.env.REPORT_RECIPIENT,
  from: process.env.VERIFIED_SENDER,
  subject: 'Weekly report',
  text: 'The weekly report is attached as a CSV file.',
  attachments: [
    {
      content: csvBytes.toString('base64'),
      filename: 'report.csv',
      type: 'text/csv',
      disposition: 'attachment'
    }
  ]
});

If you build the CSV in memory instead of reading a file, convert the string to bytes with an explicit encoding, normally UTF-8, before base64-encoding it. Avoid encoding data that is already base64. Double encoding is a common mistake, and the recipient then receives a file full of base64 text instead of rows.

For tab-separated data the steps are the same. Use a .tsv filename and the text/tab-separated-values MIME type. TSV avoids most quoting problems when values contain commas, but it breaks if a value contains a tab or a newline. Strip or escape those characters before export.

Will the file stay readable after it is opened or forwarded?

Most problems with emailed CSV files appear when a spreadsheet program opens them. The table below lists the common failure points. These are general CSV behaviors, not results tested for this article, so check them against the software your recipients actually use.

Issue | What happens | Mitigation
Character encoding | Accented letters or non-Latin scripts appear garbled if the program guesses the wrong encoding. Some spreadsheet versions do not assume UTF-8 for CSV files. | Export as UTF-8. Consider a UTF-8 byte order mark if recipients use spreadsheet software known to need it, and test it first, because some import tools treat the mark as data.
Delimiter and locale | In locales that use a comma as the decimal separator, spreadsheets may expect semicolons, so all data lands in one column. | Agree on the delimiter with recipients, or offer TSV.
Quoting | Values containing commas, quotes or line breaks split into the wrong columns if they are not quoted. | Use a CSV library that quotes fields following the common RFC 4180 conventions, rather than joining strings by hand.
Automatic type conversion | Leading zeros, long ID numbers and date-like strings can be silently changed when the file is opened. | Warn recipients, or provide the data in a typed format alongside the CSV when exact values matter.
Formula injection | Cells starting with =, +, - or @ may be interpreted as formulas by spreadsheet software. | If any values come from user input, sanitize them, for example by prefixing such cells with a single quote.

Copying values out of an opened CSV generally keeps the underlying text, because CSV holds no visual styling that could be lost. Forwarding the email preserves the attachment's bytes, but every recipient's software will apply its own interpretation again.

Accessibility depends on the recipient's spreadsheet program and assistive technology, not on SendGrid. No tested evidence was available on how specific screen readers handle CSV attachments. A short plain-text summary in the email body, giving the row count, the date range and the key figures, gives everyone the essential information without opening the file. It also helps recipients whose mail client blocks or hides attachments.

Evidence gaps

This article draws on SendGrid's general attachment model and widely documented CSV conventions. It does not include verified figures for SendGrid's size limits, delivery behavior for attachments or data retention practices, and none should be inferred from marketing material. Before you rely on attachments in production, check SendGrid's current documentation for size limits and retention, and send a test message to the mail clients and spreadsheet programs your recipients use.