Mail-merging documents from a database

You can build your TeX database, based on your needs simply by creating a list to hold the list of persons you sending your mailing. We will create a list with fields as shown in the table below, will also keep it sorted to make life easier.

enter image description here

This is done by creating first an empty list \let\alist\@empty

The format of the input file, is best to be in TeX format, to make the programming easier and to enable the use of a comma in the addresses. I have used the following format for capturing the fields, but please feel free to use your own:

\RB Nisbet|John|Mr.| 235, Highlands, Scotland | United Kingdom;
\RB Guevara|Che|Dr.| 527 Main Street, Havana | Cuba;
\RB Zapata|Emiliano|Mr.| 5237, Mexico City | Mexico;
\RB von Kleist-Schmenzin|Herr| Ewald| Greens, Dubberow| Germany;

Names are delimited by "|" and an ending semicolon. The standard part of the letter is on a separate file called stdletter.dat. I have generated it on the fly using the filecontents package. The MWE example is shown below.

\documentclass{article}
\usepackage{lstdoc,booktabs,filecontents}
\begin{filecontents}{stdletter.dat}
We are happy to enclose our new catalogue.
\vspace{20pt}

Regards,
\vspace{20pt}

Some CEO
\end{filecontents}
\begin{document}
\pagestyle{plain}
\makeatletter
\let\alist\@empty
\let\blist\@empty

\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}
}

\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}
}


\def\RB#1|#2|#3|#4|#5;{%
   \addtolist{\alist}{#1#2,}% 
   % macro for table
   \expandafter\gdef\csname#1#2@table\endcsname{\textit{#1}&#2&#3&#4\cr\relax}
   % macro for salutation
   \expandafter\gdef\csname#1#2@salut\endcsname{Dear #3 #2\relax}
   \lst@BubbleSort{\alist}
}

%% adding the data now
\RB Nisbet|John|Mr.| 235, Highlands, Scotland | United Kingdom;
\RB Guevara|Che|Dr.| 527 Main Street, Havana | Cuba;
\RB Zapata|Emiliano|Mr.| 5237, Mexico City | Mexico;
\RB von Kleist-Schmenzin|Herr| Ewald| Greens, Dubberow| Germany;

%% typesetting the table
\def\addresslist{%
\newsavebox{\tempbox}
\savebox{\tempbox}{
\centering
\begin{tabular}{llll}
  \toprule[1pt]
  First Name & Second Name & Salutation & Address\\
  \midrule
  \@for\i:=\alist \do{\csname\i @table\endcsname}
  \vspace{-14pt}\\\bottomrule
\end{tabular}}

\begin{table}
\usebox{\tempbox}
\caption{Client List}
\end{table}
}


\addresslist

\mbox{}\newpage

\@for\i:=\alist \do{\csname\i\endcsname
\csname\i \endcsname
\expandafter\csname \i @salut\endcsname
\par\medskip
\input{stdletter.dat}

\pagebreak
}

\makeatletter

\end{document}

The "article" part can be created the same way or preferably printed separately and resetting page numbering (much easier).


A simple solution is the textmerg package to obtain a single pdf, but that you can split with some external tool as pdfsam or pdftk. A MWE:

\documentclass{article}
\usepackage[utf8]{inputenc} 
\usepackage{eurosym}
\usepackage{textmerg} 
\begin{document}

\Fields{\Mr\Name\SurName\Address\Donation} 


\Merge{data.dat}{
\hspace{.25\textwidth}
\begin{tabular}{rl}
Address & {\bf \Mr\ \Name\  \SurName }\\
        & {\bf \Address }\\
        & \\
Subject & \Donation{} donation \\
\end{tabular}
\vspace{2 cm} 
Dear \Mr \Name,  bla bla bla .... 

\newpage    
}
\end{document}

Where the merged file data.dat is a plain text where the field separators are the carriage returns as in this example:

Mr.
Peter
Smith
Newtown Road, Los Angeles, USA
209 \$


Ms. 
Maria 
Rossi
Regina Elena, 113, Milano, Italy
145 \euro