8

I'm sending the same letter to multiple addresses. The addresses are stored in a csv document. I've so far created a loop to create multiple letters, but I have not been successful in finding a format to pull the address from the csv.

Here is the csv format

Name    Address City, State  Zip
Company 1   5555 N 5th St   Townsville, CA  55555
Company 2   5556 N 5th St   Townsville, CA  55556
Company 3   5557 N 5th St   Townsville, CA  55557

Here's the idea, but I don't know how to call specific elements within a csv

\documentclass[11pt]{letter}
\usepackage{csvsimple}

\usepackage{multido}
\newcommand{\forLoop}[4][1]{\multido{\i=#2+#1}{#3}{#4}}
\begin{document}
\csvreader[head to column names]{sample.csv}{} %

\forLoop[1]{2}{4}
{ 

\begin{letter}{csv[\i\,1]\\csv[\i\,2]\\csv[\i\,3]}
\begin{letter}
\opening{Dear ConAgra Food Supplier:}
\closing{Best Regards,\\[2ex]%
{
\LARGE\calligra Jane Doe}\\[2ex]%
Jane Doe\\Title\\Company\\Phone #
}
\end{letter}
}

\end{document}
6

I would suggest using the datatool package (click the image to enlarge it):

enter image description here

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{sample.csv}
Name, Address, City, State, Zip
Company 1, 5555 N 5th St, Townsville, CA, 55555
Company 2, 5556 N 5th St, Townsville, CA, 55556
Company 3, 5557 N 5th St, Townsville, CA, 55557
\end{filecontents*}

\usepackage{datatool,lipsum}

\pagestyle{empty}
\setlength{\parindent}{0pt}
\begin{document}

\DTLloaddb{sample}{sample.csv}

\DTLforeach{sample}{%
  \Name=Name,\Address=Address,\City=City,\State=State,\Zip=Zip}{%
  \clearpage

  \hfill
  \begin{tabular}{l@{}}
    \today\\[10pt]
    Who from \\
    Street \\
    City, State \\
    Phone: (123) 456-7890 \\
    Email: email@mail.com
  \end{tabular}

  \bigskip

  \begin{tabular}{@{}l}
    \Name \\
    \Address \\
    \City \\
    \State~\Zip
  \end{tabular}

  \bigskip

  To whom it may concern,

  \bigskip

  \lipsum[1]

  \bigskip

  Sincerely,

  \bigskip\bigskip\bigskip

  First~Last
}

\end{document}

I'd forego the letter document class for something more simple or straight-forward, like the article class. The layout is easily manipulated and a mail-merge style production of multiple letters to different addresses is easily cycled through using \DTLforeach.

  • Are there any way to get pdf files (file names follow one of the columns) separated when compile the file? – Joe Feb 13 '17 at 9:38
3

I think @Werner posted a good answer using datatool. Since the OP used csvsimple, here is the adaption to csvsimple with the same output. There are only very few syntactical changes. For this application, datatool and csvsimple are mutually exchangeable.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{sample.csv}
Name, Address, City, State, Zip
Company 1, 5555 N 5th St, Townsville, CA, 55555
Company 2, 5556 N 5th St, Townsville, CA, 55556
Company 3, 5557 N 5th St, Townsville, CA, 55557
\end{filecontents*}

\usepackage{csvsimple,lipsum}

\pagestyle{empty}
\setlength{\parindent}{0pt}
\begin{document}

\csvreader[head to column names]{sample.csv}{}{%
  \clearpage

  \hfill
  \begin{tabular}{l@{}}
    \today\\[10pt]
    Who from \\
    Street \\
    City, State \\
    Phone: (123) 456-7890 \\
    Email: email@mail.com
  \end{tabular}

  \bigskip

  \begin{tabular}{@{}l}
    \Name \\
    \Address \\
    \City \\
    \State~\Zip
  \end{tabular}

  \bigskip

  To whom it may concern,

  \bigskip

  \lipsum[1]

  \bigskip

  Sincerely,

  \bigskip\bigskip\bigskip

  First~Last
}

\end{document}
1

I'd like to suggest a slightly different approach. Below you can find a letter defined as macro \mailto in the preamble of the document that is called for each recipient in the body of the document. Hence, I don't read the address from a CSV file but put it into the main document. You still get the idea how each letter is generated from the macro call.

You can easily customize this with more macro parameters such that you can pass more information to the letter template. I'm using the dinbrief class but this works with any other letter class as well.

\documentclass[11pt]{dinbrief} 

\signature{Dr. Christian Lindig}
\place{Boston}
\nowindowrules

\long\def\mailto#1#2{\begin{letter}{#2}
\subject{Samples for Pop-Fizz}
\opening{#1,}

please find enlclosed the samples that you requested.

\closing{With kind regards}
\end{letter}}


\begin{document}
\mailto{Dear Dr. Drofnuts}{Dr. Drofnuts\\Computer Science Dept\\St. Anford, CA\\USA}
\mailto{Dear Gregor Samsa}{Gregor Samsa\\Ul. Radnice 5\\Prague\\Czech Republic}
\end{document}

enter image description here

0

I would suggest to treat the CSV file externally and put them in the letter directly like so:

\documentclass[11pt]{letter}

\address{Jane Doe\\Title\\Company\\Phone \#}
\signature{Jane Doe}

\begin{document}

\newcommand\writeletter[4]{
    \begin{letter}{#1 #2\\#3\\#4}
        \opening{Dear #1!}
        Here's a very long text to #1 at #2 in #3.
        \closing{With best regards to #3}
    \end{letter}
}

\writeletter{Company 1}{5555 N 5th St}{Townsville, CA}{55555}
\writeletter{Company 2}{5556 N 5th St}{Townsville, CA}{55556}
\writeletter{Company 3}{5557 N 5th St}{Townsville, CA}{55557}

\end{document}

The example is entirely based on the german Wikibooks page on \LaTeX serial letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.