Every new recipient is inserted in a sequence that will serve as a basis for the mapping; the name and the address will be put in a property list.
The text of the letter is given as argument to \printletter that cycles over the items in the sequence; inside the letter, \name and \address will expand to the current recipient's, while \cc will print an itemized list of all other recipients' names.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newrecipient}{mm}
{% #1 = name, #2 = address
\clement_new_recipient:nn { #1 } { #2 }
}
\NewDocumentCommand{\name}{}
{
\prop_item:Nf \g_clement_recipients_prop { \l_clement_current_recipient_tl @ name }
}
\NewDocumentCommand{\address}{}
{
\prop_item:Nf \g_clement_recipients_prop { \l_clement_current_recipient_tl @ address }
}
\NewDocumentCommand{\cc}{}
{
\clement_cc_list:
}
\NewDocumentCommand{\printletter}{+m}
{
\seq_map_inline:Nn \g_clement_recipients_seq
{
\tl_set:Nn \l_clement_current_recipient_tl { ##1 }
#1
\clearpage
}
}
\seq_new:N \g_clement_recipients_seq
\prop_new:N \g_clement_recipients_prop
\tl_new:N \l_clement_current_recipient_tl
\cs_generate_variant:Nn \prop_gput:Nnn { Nfn }
\cs_generate_variant:Nn \prop_item:Nn { Nf }
\cs_generate_variant:Nn \tl_if_eq:nnF { V }
\cs_new_protected:Npn \clement_new_recipient:nn #1 #2
{
\seq_gput_right:Nx \g_clement_recipients_seq { \tl_to_str:n { #1 } }
\prop_gput:Nfn \g_clement_recipients_prop
{ \tl_to_str:n { #1 } @ name } { #1 }
\prop_gput:Nfn \g_clement_recipients_prop
{ \tl_to_str:n { #1 } @ address } { #2 }
}
\cs_new_protected:Npn \clement_cc_list:
{
\begin{itemize}
\seq_map_inline:Nn \g_clement_recipients_seq
{
\tl_if_eq:VnF \l_clement_current_recipient_tl { ##1 }
{
\item \prop_item:Nf \g_clement_recipients_prop { ##1 @ name }
}
}
\end{itemize}
}
\ExplSyntaxOff
\newrecipient{Toto}{That Street\\Town}
\newrecipient{Tata}{This Street\\Village}
\newrecipient{T\'et\'e}{Another Place}
\begin{document}
\printletter{
\hspace*{\fill}%
\begin{tabular}{l@{}}
\name \\
\address
\end{tabular}
\bigskip
Here, \today
\bigskip
\bigskip
Dear \name,\\
here is my letter to you and to other people that
you'll find listed at the bottom. Some other words
in order to fill some lines.
\bigskip
Sincerely yours
A. Uthor
\bigskip
\cc
}
\end{document}
Of course, the complex code will be stored in an external file to be \input, so it's reusable and it won't clobber the main document.
The shown printout uses a rule instead of the \clearpage in the code, so as to fill just a page.
