Mail Merging with newlfm.cls, envlab.sty and datatool.sty
\documentclass[stdletter,nocapaddress,avery5164biglabel]{newlfm}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[british]{babel}
\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}
\newlfmP{orderfromtodate,sigcenter,addrfromphone,addrfromemail}
\namefrom{Mr Big Head}
\addrfrom{University of Somewhere\\Some City\\AB3 4YZ}
\emailfrom{big.head@somewhere.ac.uk}
\phonefrom{0123456789}
\regarding{A sample letter}
\closeline{Yours sincerely}
\cclist{Prof Important Person\\Dr Bor Ing}
\encllist{Photocopy of something interesting\\
Photocopy of something rather dull}
\psitem{this is a postscript}
\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%
}
{%
\ifcsbool{\Subscribed}
{%
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}
\nameto{\DTLifnullorempty{\Title}{}{\Title\ }\Forenames\ \Surname}
\addrto{%
\AddressI\\
\DTLifnullorempty{\AddressII}{}{\AddressII\\}\Town\\
\DTLifnullorempty{\County}{}{\County\\}\Postcode\\\CountryName
}
\greetto{Dear \DTLifnullorempty{\Title}{\Forenames}{\Title} \Surname}
\begin{newlfm}
This is an imaginary letter.
This is the second paragraph of the letter.
\end{newlfm}
}%
{}% not subscribed
}
\startlabels
\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%
}
{%
\ifcsbool{\Subscribed}
{%
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}
\mlabel
{Mr Big Head\\University of Somewhere\\Some City AB3 4YZ}%
{\DTLifnullorempty{\Title}{}{\Title\ }\Forenames\ \Surname\\%
\AddressI\\
\DTLifnullorempty{\AddressII}{}{\AddressII\\}\Town\\
\DTLifnullorempty{\County}{}{\County\\}\Postcode\\\CountryName
}
}%
{}% not subscribed
}
\end{document}
Download mail-merging-newlfm-labels.tex or mail-merging-newlfm-labels.pdf.
