E-Mails mit personalisierten Attachments versenden
Vor kurzem hatte ich die dankbare Aufgabe, mehrere hundert E-Mails mit personalisierten Anhängen (im PDF-Format) zu versenden. Da auf die Schnelle kein Tool bereitstand, das bei dieser Aufgabe helfen konnte, musste etwas Selbstentwickeltes her.
Teil 1 der Aufgabe bestand darin, aus der PDF-Gesamtdatei jeweils zwei Seiten zu extrahieren und abzuspeichern. In einer Excel standen glücklicherweise die Namen der Kunden sowie die Kunden-ID. Die Exceldatei habe ich dann als CSV exportiert (man hätte die Excel-Datei sicher auch direkt lesen können) und ein Powershellskript für PDFTK geschrieben.
# Uwe Ziegenhagen, 20101207 # read addresses, tab-delimited $entries = Import-Csv -Delimiter "`t" c:\adresses.txt # change to directory of PDF pages cd c:\Email_Sende_Aktion\ # user counter variable ($i contains an object) $j = 0 foreach ($i in $entries){ $j = $j +1 # take filename of new PDF from CSV $filename = $i.addressid + ".pdf" # calculate pages to be extracted $pagestoextract = "A" + $j + "-" + ($j+1) # call pdftk pdftk A= bigserialletter.pdf cat $pagestoextract output $filename $j = $j +1 } |
Teil 2 der Aufgabe bestand dann darin, jedem Kunden eine E-Mail mit seinem Attachment zu schicken, kein Kunde sollte ein PDF erhalten, das nicht für ihn bestimmt war. Dank Powershells „send-mailmessage“ ließ sich das auch recht einfach umsetzen.
# generate Encoding object for UTF8 (no äüö otherwise) $encoding = [System.Text.Encoding]::UTF8 # Import email addresses and fund ids $files = Import-Csv -Delimiter "`t" c:\Email_Sende_Aktion\adresses.txt # read mail body, preserve line breaks # http://www.eggheadcafe.com/software/aspnet/33110753/passing-a-string-with-linebreaks-to-a-com-object.aspx $body = [io.file]::ReadAllText("c:\Email_Sende_Aktion\mailbody.txt") # just a counter for checking $t = 0; foreach ($i in $files){ # inc counter $t = $t +1 # cat body from the different parts $usedbody = "Sehr geehrte/r " + $i.Anrede.Trim() + ". " + $i.Nachname.Trim() + "," + $body # find the individual attachment based on addressid $file = "c:\Email_Sende_Aktion\" + $i.addressid + ".pdf" # send message send-mailmessage -Encoding $encoding -From me@me.com -BCC notme@me.com -To $i.RealMail -Subject "Kundeninformation Foobar" -Body $usedbody -Smtpserver nameofexchangeserver -Attachments $file $t } |
English summary: If you have no dedicated tool to send personalized PDF-attachments via e-mail, you can easily use Powershell to generate theses e-mails. Part 1 of the task consisted of splitting a big PDF file with all serial letters into 2-page pieces, task 2 of sending the e-mails to the recipients.
Keywords: Powershell, Outlook, Attachment, PDF