[Tex/LaTex] Mail merge using datatool with letter class

datatoolletters

I am trying to adapt this question/answer on how to do mail merge to work with the letter document class.

The linked answer works great for me, but when I try to change the document class to letter I keep getting an error| Argument on \@tempc has an extra }, but it seems that my braces are balanced. Are letter and datatool incompatible? Here is my example. How can I better understand this error? Thanks.

\documentclass{letter}
\usepackage{datatool}
\usepackage{parskip}

\signature{Richard Herron}
\address{New York, NY}

\makeatletter
% Blank/missing fields commands
% \skipblank adds \\ to filled field
% * version adds \space instead of newline
\newcommand\skipblank{\@ifstar\@spskip\@nlskip}
\newcommand\@nlskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\\}}
\newcommand\@spskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\space}}
% \checkblank replaces blank fields with ***
\newcommand\checkblank[1]{\ifthenelse{\DTLiseq{#1}{}}{***}{#1}}
\makeatother

\begin{document}

\DTLloaddb{addresses}{mail-merge-addresses.csv} 
\DTLforeach*{addresses}{%
    \addtitle=Title,
    \firstname=FirstName,
    \lastname=LastName,
    \position=Position,
    \addressi=Address1,
    \addressii=Address2,
    \addressiii=Address3,
    \city=City,
    \state=State,
    \zip=Zip,
\country=Country}
{%
    \begin{letter}{\checkblank{\addtitle}\ \firstname\ \lastname\\
        \skipblank{\position}
        \skipblank{\addressi}
        \skipblank{\addressii}
        \skipblank{\addressiii}
        \city, \state\ \zip\\
        \skipblank*{\country}
    }

    \opening{Dear \checkblank{\addtitle}\ \lastname,}

    This is the text of the letter.

    \closing{Sincerely,}

\end{letter}
}

\end{document}

Edit: I should add that I'm using the same .csv file database that the linked answer uses, but I get the same results if I remove the blank entries.

Title,  LastName,   FirstName,  Position,   Address1,   Address2,   Address3,   City,   State,  Zip,    Country
Dr.,    Smith,  James,  Chair,  Dept. of Physics,   University of Somewhere,    434 East Hall,  Cambridge,  MA, 02139,  
Mr.,    Jones,  Bill,   Director of Human Resources,    ,   University of Nowhere,  A203 King St.,  Stanford,   CA, 94305,  
,   Brown,  Sally,  ,   Firstline,  ,   3rdline,    East Lansing,   MI, ,   USA

Best Answer

The argument to \begin{letter} is massaged in order to extract the addressee. This massage is incompatible with \ifthenelse.

Here's a way out: we fed the argument in “explicit” form.

\documentclass{letter}
\usepackage{datatool}
\usepackage[businessenvelope]{envlab}
\makelabels

\signature{Richard Herron}
\address{New York, NY}

\makeatletter
% Blank/missing fields commands
% \skipblank adds \\ to filled field
% * version adds \space instead of newline
\newcommand\skipblank{\@ifstar\@spskip\@nlskip}
\newcommand\@nlskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\\}}
\newcommand\@spskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\space}}
% \checkblank replaces blank fields with ***
\newcommand\checkblank[1]{\ifthenelse{\DTLiseq{#1}{}}{***}{#1}}

\newtoks\address@toks
\newcommand\addto@address[1]{%
  \address@toks=\expandafter{\the\expandafter\address@toks#1}%
}
\newcommand\makeaddress{%
  \address@toks={}%
  \ifthenelse{\DTLiseq{\addtitle}{}}{\addto@address{***\ }}{\addto@address{\addtitle\ }}%
  \addto@address{\firstname\ }%
  \addto@address{\lastname}%
  \addto@address{\noexpand\\}
  \ifthenelse{\DTLiseq{\position}{}}{}{\addto@address{\position\\}}
  \ifthenelse{\DTLiseq{\addressi}{}}{}{\addto@address{\addressi\\}}
  \ifthenelse{\DTLiseq{\addressii}{}}{}{\addto@address{\addressii\\}}
  \ifthenelse{\DTLiseq{\addressiii}{}}{}{\addto@address{\addressiii\\}}
  \addto@address{\city, }\addto@address{\state\ }\addto@address{\zip}
  \ifthenelse{\DTLiseq{\country}{}}{}{\addto@address{\noexpand\\}}
  \ifthenelse{\DTLiseq{\country}{}}{}{\addto@address{\country}}
  \begingroup
  \edef\x{\endgroup\noexpand\begin{letter}{\the\address@toks}}\x
}
\makeatother

\begin{document}

\DTLloaddb{addresses}{mail-merge-addresses.csv} 
\DTLforeach*{addresses}{%
    \addtitle=Title,
    \firstname=FirstName,
    \lastname=LastName,
    \position=Position,
    \addressi=Address1,
    \addressii=Address2,
    \addressiii=Address3,
    \city=City,
    \state=State,
    \zip=Zip,
\country=Country}
{%
 \makeaddress
    \opening{Dear \checkblank{\addtitle}\ \lastname,}

    This is the text of the letter.

    \closing{Sincerely,}

\end{letter}
}

\end{document}
Related Question