Exercise 10: Mail Merging using the isodoc Class (Solution)
This is a solution to Exercise 10 using the isodoc class instead of the scrlttr2 class. You need the sample files people.csv and country-codes.csv. Alternatively, if you want to use data from the samples SQL database, remember to use datatooltk to import the data to a .dbtex file and load it using \DTLloaddbtex as described in Loading Data From a .dbtex File.
\documentclass[12pt]{isodoc}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{datatool}
\newcommand{\ifcsbool}[3]{%
\ifboolexpr
{
test{\ifdefstring{#1}{true}} or
test{\ifdefstring{#1}{1}}
}
{#2}{#3}%
}
\DTLloaddb{people}{people.csv}
\DTLloaddb{countries}{country-codes.csv}
\setupdocument
{%
language={en-GB},%
company={University of Somewhere},%
who={Mr Big Head},
street={Academic Lane},%
city={Some City},%
zip={AB3 4YZ},%
country={United Kingdom},%
countrycode={GB},%
areacode={44},%
cityzip,%
subject={A sample letter},%
closing={Yours sincerely},%
enclosures={Photocopy of something interesting\\
Photocopy of something rather dull},%
signature={Big Head, Managing Director},%
autograph=1,%
copyto={Prof Important Person\\Dr Bor Ing},%
footer,%
phone={123456789},%
cellphone={712345678},%
email={big.head@somewhere.ac.uk},%
website={somewhere.ac.uk},%
date={2014-03-01}%
}
\begin{document}
\DTLforeach*{people}% data
{% assignments
\Id=id,%
\Surname=surname,%
\Forenames=forenames,%
\Title=title,%
\AddressI=address1,%
\AddressII=address2,%
\Town=town,%
\County=county,%
\Postcode=postcode,%
\CountryCode=country,%
\Subscribed=subscribed%
}
{% body
\ifcsbool{\Subscribed}
{%
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}
\ifdefstring{\CountryCode}{gb}{\foreignfalse}{\foreigntrue}
\letter
[%
opening={Dear \DTLifnullorempty{\Title}{\Forenames}{\Title} \Surname},%
to={\DTLifnullorempty{\Title}{}{\Title\ }\Forenames\ \Surname\\
\AddressI\\
\DTLifnullorempty{\AddressII}{}{\AddressII\\}%
\Town\\%
\DTLifnullorempty{\County}{}{\County\\}%
\Postcode\ifdefstring{\CountryCode}{gb}{}{\\\CountryName}}
]
{%
This is an imaginary letter.
This is the second paragraph of the letter.
}%
}%
{}% not subscribed
}
\end{document}
Download mail-merging-isodoc.tex or mail-merging-isodoc.pdf.
