Scrlttr2 and datatool compatability?

I'm interested in writing some form letters. I'm a big fan of scrletter2's features, notably placing the "to" address in just the right place for business envelopes.

For the mail merge, I've used datatools package in the past. It's pretty straightforward to use in conjunction with the article class. A MWE might look like

\documentclass{letter}
\usepackage{datatool}
\begin{document}
\DTLforeach{scores}{\firstname=FirstName,\surname=Surname,\score=Score}{%

\begin{letter}{}
\opening{Dear \firstname\ \surname}
Congratulations on your score of \score !
\closing{Yours Sincerely}
\end{letter}

}
\end{document}


However, with scrletter2 things get hairy. Specifically, I'm having trouble figuring out how to place my commands.

I don't know how to post a MWE that's not horrendously long, but here's the gist:

\documentclass[11pt,english]{scrlttr2}
\usepackage{datatool}
\begin{document}

% a bunch of \setkomavar{fromname}{From name} and similar

\DTLforeach{info}{\firstname=FirstName,
\surname=LastName,
\position=Position,}{%

\begin{letter}{\firstname}
Text here
\end{letter}
}
\end{document}


I get the following error:

Runaway argument?
\@nil \@@ {scores}\fi \ifthenelse {\boolean {true}}{\refstepcounter {\ETC.
Paragraph ended before \@dtl@next was complete.
\par
l.130 }


I feel like something's wrong with the placement of my \DTL commands within the document, but can't figure anything out.

• Can you add a sample StudentScores.csv file? – egreg Jul 2 '15 at 16:00
• egreg not uploading because esdd answered the question, I should have included in MWE – mbarete Jul 3 '15 at 18:56

You have to enclose the letter environment by the \DTLforeach command or the DTLenvforeach environment. But you have to be accurate with the assign list: there should be no additional space before or after a key and no comma at the end of the assign list.

Code with the environment DTLenvforeach:

\begin{filecontents*}{test.csv}
FirstName,LastName,Score
John,"Smith, Jr",68
Jane,Brown,75
Andy,Brown,42
Clare,Verdon,45
\end{filecontents*}

\documentclass[
english,
fromalign=right
]{scrlttr2}
\usepackage{babel}
\usepackage{datatool}

\begin{document}
\setkomavar{fromname}{Sydney Summer}

\begin{DTLenvforeach}{info}{\firstname=FirstName,\surname=LastName,\score=Score}
\begin{letter}{\firstname{} \surname}
\opening{Dear \firstname,}
Congratulations on your score of \score !
\closing{Yours sincerely}
\end{letter}
\end{DTLenvforeach}
\end{document}


Or with the command \DTLforeach:

\documentclass[
english,
fromalign=right
]{scrlttr2}
\usepackage{babel}
\usepackage{datatool}

\begin{document}
\setkomavar{fromname}{Sydney Summer}