How to Send Bulk Emails from Excel with VBA (Gmail and Attachments)
To send bulk emails from Excel with VBA, loop through your rows, replace fields like {{First name}} in the subject and text with each row’s values, and send every email with CDO through smtp.gmail.com on port 465 using a Gmail app password. The macro below adds attachments, skips bad and duplicate addresses, and writes a status per row.
Table of contents
How VBA sends email
VBA has no email command of its own. It uses CDO, a mail library that ships with Windows, to talk to an SMTP server such as Gmail’s. The macro reads each row, builds one message, and hands it to smtp.gmail.com with your Gmail address and an app password.
The video above shows Gmail Blaster, the Excel template I sold at the time. Its sending part used exactly this technique: a CDO configuration pointing at Gmail on port 465 with SSL, then one CDO message per row. This post gives you that core as a short macro you can paste into your own workbook and adapt. Gmail Blaster itself is retired; more on that at the end.
Because CDO is part of Windows, the macro needs no add-in and no extra reference. It does not run on a Mac, where CDO does not exist.
What you need
You need desktop Excel on Windows, a workbook saved as .xlsm so it can hold macros, and a Gmail or Google Workspace account with 2-Step Verification and an app password. Gmail refuses your normal password from scripts and macros.
-
Create a Gmail app password. Turn on 2-Step Verification in your Google account, open App passwords, type a name such as Excel VBA and click Create. Copy the 16-letter password.
-
Show the Developer tab. In Excel, go to File, Options, Customize Ribbon and tick Developer.
-
Add a module. Press Alt+F11 to open the VBA editor, then Insert, Module, and paste the code from this post.
-
Save as .xlsm. Use File, Save As and pick Excel Macro-Enabled Workbook.
The app password sits in the code as plain text, so anyone who opens the VBA editor can read it. Keep the workbook to yourself, or delete the app password in your Google account when you no longer need it.
Set up the workbook
Use one sheet called Contacts with column names in row 1 and one person per row, including an Email column. Add a second sheet called Message with the subject in cell B1 and the email text in cell B2. Any column name in two curly brackets becomes a field.
The Contacts sheet of my free template already has the right layout: First name, Last name, Email, Company, City, Due date, Amount due and Invoice file. Download it, save it as .xlsm, add the Message sheet and you are ready.
Mail merge contact list template
The Contacts sheet the macro reads, with sample rows and a Check column that flags missing, broken and duplicate addresses. Save it as .xlsm and add the Message sheet.
On the Message sheet, B1 could say Your invoice for {{Company}} and B2 could hold the text below. Press Alt+Enter inside the cell for a new line.
Hi {{First name}},
your invoice of {{Amount due}} is due on {{Due date}}.
Best,
Sam
The macro fills fields with the cell text exactly as Excel shows it, so number and date formats carry over. Format the Amount due column as currency and the email says $480.00. If a column is too narrow, Excel shows ### and so would the email, so widen the column first.
The VBA code
The macro below sends one email per row of the Contacts sheet through Gmail. Change the settings at the top, run SendEmailsFromList from the Developer tab under Macros, and read the Status column it adds. Test with your own address in the list first.
Option Explicit
' --- Settings ---------------------------------------------------------
Const GMAIL_ADDRESS As String = "you@gmail.com"
Const APP_PASSWORD As String = "abcdefghijklmnop" ' 16-letter app password
Const LIST_SHEET As String = "Contacts"
Const MESSAGE_SHEET As String = "Message" ' B1 = subject, B2 = text
Const ATTACHMENT_FOLDER As String = "C:\Invoices\"
Const DELAY_SECONDS As Long = 5
' ----------------------------------------------------------------------
Sub SendEmailsFromList()
Dim ws As Worksheet, lastRow As Long, lastCol As Long
Dim r As Long, c As Long, sent As Long
Dim colEmail As Long, colFile As Long, colStatus As Long
Dim subjectText As String, bodyText As String
Dim toAddress As String, fileName As String
Dim cdoConfig As Object, msg As Object, seen As Object
Dim schema As String
Set ws = ThisWorkbook.Worksheets(LIST_SHEET)
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
colEmail = ColumnOf(ws, "Email")
colFile = ColumnOf(ws, "Invoice file")
colStatus = ColumnOf(ws, "Status")
If colEmail = 0 Then
MsgBox "No column called Email in row 1 of " & LIST_SHEET & "."
Exit Sub
End If
If colStatus = 0 Then
colStatus = lastCol + 1
ws.Cells(1, colStatus).Value = "Status"
End If
' One SMTP configuration for all emails: Gmail, SSL on port 465
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(schema & "sendusing") = 2
.Item(schema & "smtpserver") = "smtp.gmail.com"
.Item(schema & "smtpserverport") = 465
.Item(schema & "smtpusessl") = True
.Item(schema & "smtpauthenticate") = 1
.Item(schema & "sendusername") = GMAIL_ADDRESS
.Item(schema & "sendpassword") = APP_PASSWORD
.Item(schema & "smtpconnectiontimeout") = 30
.Update
End With
Set seen = CreateObject("Scripting.Dictionary")
For r = 2 To lastRow
toAddress = LCase(Trim(ws.Cells(r, colEmail).Text))
If InStr(toAddress, "@") = 0 Or InStr(InStr(toAddress, "@") + 1, toAddress, ".") = 0 Then
ws.Cells(r, colStatus).Value = "Skipped: no valid email address"
ElseIf seen.Exists(toAddress) Then
ws.Cells(r, colStatus).Value = "Skipped: already on the list in row " & seen(toAddress)
Else
seen.Add toAddress, r
subjectText = ThisWorkbook.Worksheets(MESSAGE_SHEET).Range("B1").Value
bodyText = ThisWorkbook.Worksheets(MESSAGE_SHEET).Range("B2").Value
' Replace {{Column name}} with this row's value, as shown in Excel
For c = 1 To lastCol
subjectText = Replace(subjectText, "{{" & ws.Cells(1, c).Value & "}}", _
ws.Cells(r, c).Text, 1, -1, vbTextCompare)
bodyText = Replace(bodyText, "{{" & ws.Cells(1, c).Value & "}}", _
ws.Cells(r, c).Text, 1, -1, vbTextCompare)
Next c
Set msg = CreateObject("CDO.Message")
Set msg.Configuration = cdoConfig
msg.From = GMAIL_ADDRESS
msg.To = toAddress
msg.Subject = subjectText
msg.TextBody = bodyText
fileName = ""
If colFile > 0 Then fileName = Trim(ws.Cells(r, colFile).Text)
If fileName <> "" And Not FileExists(ATTACHMENT_FOLDER & fileName) Then
ws.Cells(r, colStatus).Value = "Not sent: file not found " & fileName
Else
If fileName <> "" Then msg.AddAttachment ATTACHMENT_FOLDER & fileName
On Error Resume Next
msg.Send
If Err.Number = 0 Then
ws.Cells(r, colStatus).Value = "Sent " & Format(Now, "yyyy-mm-dd hh:nn")
sent = sent + 1
Else
ws.Cells(r, colStatus).Value = "Not sent: " & Err.Description
End If
On Error GoTo 0
If r < lastRow Then Application.Wait Now + TimeSerial(0, 0, DELAY_SECONDS)
End If
Set msg = Nothing
End If
Next r
MsgBox sent & " emails sent. See the Status column for details."
End Sub
Private Function ColumnOf(ws As Worksheet, header As String) As Long
Dim c As Long
For c = 1 To ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
If LCase(Trim(ws.Cells(1, c).Value)) = LCase(header) Then
ColumnOf = c
Exit Function
End If
Next c
End Function
Private Function FileExists(path As String) As Boolean
FileExists = (Dir(path) <> "")
End Function
I tested this code with the template’s sample rows against a local test mail server: four emails went out with the right fields and files, the missing invoice and the two bad addresses were reported, and the duplicate row was skipped. The Status column after that run:
| Status | |
|---|---|
| anna.berger@example.com | Sent 2026-11-28 11:08 |
| erin.fox@example.com | Not sent: file not found Invoice-1045.pdf |
| (empty) | Skipped: no valid email address |
| grace.hill@example | Skipped: no valid email address |
| ben.carter@example.com | Skipped: already on the list in row 3 |
How the macro works
The macro finds the Email, Invoice file and Status columns by their names in row 1, sets up one CDO configuration for Gmail, then loops through the rows. For each valid, new address it fills the fields, attaches the file if there is one, sends, writes the result and waits a few seconds.
- Settings. The constants at the top hold your Gmail address, the app password, the sheet names, the folder with the attachments (keep the backslash at the end) and the pause between emails.
- ColumnOf. Looks up a column by its heading, so you can reorder columns without touching the code. If there is no Status column, the macro adds one after the last column.
- CDO configuration.
sendusing = 2means “send through an SMTP server”. Server smtp.gmail.com, port 465 andsmtpusessl = Truegive an encrypted connection, andsmtpauthenticate = 1logs in with your address and app password. - Fields. For each column,
Replaceswaps{{Heading}}for that row’s cell text.vbTextComparemakes it ignore upper and lower case. - Checks. An address needs an @ and a dot after it. A Scripting.Dictionary remembers every address already handled, so a second row with the same address is skipped.
- Errors.
On Error Resume Nextaroundmsg.Sendcatches a failed send, and the error text goes into the Status column instead of stopping the whole run.
To send HTML instead of plain text, use msg.HTMLBody instead of msg.TextBody and write the text with HTML tags such as p and b. To send the same file to everyone, put that file name in every row of the Invoice file column.
Send through Outlook instead
Classic Outlook on Windows works too. Instead of CDO, VBA creates a mail item with Outlook’s object model and calls Send. Emails then go out from your default Outlook account and land in Sent Items. The new Outlook for Windows cannot be automated this way.
The changes are small: create the application with CreateObject("Outlook.Application"), make each email with .CreateItem(0), set .To, .Subject and .Body, add files with .Attachments.Add, and call .Send. The loop, the fields and the checks stay the same. Outlook may show a security prompt when another program sends on your behalf, depending on your antivirus and Outlook settings.
If you would rather do this in Python, the same loop with smtplib is in my post on sending Gmail from Excel with Python, and my tutorial on creating and emailing Excel files with Python and Outlook uses Outlook from Python.
Troubleshooting
Most failures are the login, the network or the file path. Gmail rejects a normal password, a typo in the app password, or an account without 2-Step Verification. Some networks block port 465. A missing backslash in the attachment folder makes every file “not found”.
- “The transport failed to connect to the server”. Port 465 is blocked or the server name is wrong. Try another network, and check
smtp.gmail.comand 465. - “The transport error code was 0x80040217. The server response was not available”. Gmail refused the login. Use an app password, not your normal password, and copy it without spaces.
- Every file is “not found”. The folder path needs a backslash at the end, for example
C:\Invoices\, and the file names must match exactly, including .pdf. - A field stays as {{Name}} in the email. The heading in row 1 is spelled differently, or has an extra space. Upper and lower case do not matter, spaces do.
- Excel freezes while sending. That is the pause between emails. Excel is busy until the macro ends, so let it finish.
- The macro does not run at all. The file is saved as .xlsx, or macros are blocked. Save as .xlsm and allow macros for this file. A workbook downloaded from the internet may need Unblock in its file properties first.
Limits
Gmail allows about 500 emails a day from a free account and about 2,000 from Google Workspace, whatever sends them. The macro also runs only on Windows, freezes Excel while it works, and has no preview: you see the result after sending, not before.
That is fine for a monthly list of a few dozen people. It gets risky when a typo in the text goes to 300 customers, when the list is bigger than one day’s Gmail limit, or when you need more than one attachment column, CC and BCC, or a table with each person’s own rows.
Gmail Blaster did much more than this macro: spin text, HTML signatures and Excel ranges in the email. It is no longer sold. Its successor is SheetCourier, a Windows app I built that reads your Excel file and sends through Gmail, Outlook or another account. It adds a check step, a preview for every person, a daily limit counter, and per-person tables. I compare the options in my guide to mail merge from Excel to Outlook or Gmail, and the Gmail side in sending personal emails from Gmail with an Excel list.
The no-code successor of Gmail Blaster: one personal email per Excel row through your own Gmail, Outlook or other account, with a preview, a test to yourself and per-person attachments and tables. Try it with 100 free emails.
