<?xml version="1.0" encoding="windows-1252"?>
<node id="689054" title="Send mail with attachment using only SMTP" created="2008-05-29 13:06:51" updated="2008-05-29 09:06:51">
<type id="115">
perlquestion</type>
<author id="647343">
Mad Ant</author>
<data>
<field name="doctext">
&lt;p&gt;Hello fellow Monks.&lt;/p&gt;

&lt;p&gt;I need to write a script which checks maintenance for a program running on a distant machine. I have limited rights on this machine, so no installing various CPAN modules.&lt;/p&gt;

&lt;p&gt;The mail needs to be sent with data in the mail body, and the same data in a CSV file that will be sent as an attachment.&lt;br&gt;
I thought I had it down pat, but the mail sends, and their is an attachment, but it is almost empty, unreadable file.&lt;/p&gt;

&lt;p&gt;Could someone please tell me where I'm going wrong ? Any help is much appreciated.&lt;/p&gt;

&lt;p&gt;Here's the code refering to the CSV file :
&lt;code&gt;
#!/usr/bin/perl

use strict;
use DBI;
use Net::SMTP;
use utf8;

my @addr_to_send = qw/anton.harris@beirtelecom.com/;
my $from_addr = 'systemplus@joursheureux.be';
my $smtp_addr = 'smtp.skynet.be';
my $csvfile = "appelerr.csv";

... # gets info from a database, creates a CSV file

foreach my $to_send (@addr_to_send) {
print "Sending mail to $to_send\n";
my $smtp = Net::SMTP-&gt;new($smtp_addr);
$smtp-&gt;mail($from_addr);
$smtp-&gt;to($to_send);
$smtp-&gt;data();
$smtp-&gt;datasend("Subject: Appels S+ erronés");
$smtp-&gt;datasend("\n");
$smtp-&gt;datasend("MIME-Version: 1.0\n");
$smtp-&gt;datasend("Content-type: multipart/mixed;
boundary=\"frontier\"\n");
$smtp-&gt;datasend("--frontier\n");
$smtp-&gt;datasend("Content-type: text/plain\n");
$smtp-&gt;datasend("Explication sur cet e-mail :\n");
$smtp-&gt;datasend("Cet e-mail contient la liste des appels effectués par les pensionnaires et qui ne se sont pas terminés normalement.\n");
$smtp-&gt;datasend("Il peut s'agir d'un moment où l'infirmière a pu résoudre les raisons de l'appel sans devoir appeler la chambre,\n");
$smtp-&gt;datasend("puis effectue un reset, ou que le pensionnaire, impatient, sonne de manière répétitive,\n");
$smtp-&gt;datasend("mais cela peut également etre un temps de réponse beaucoup trop long.\n\n");
if ($#badcalls &lt; 0) {
$smtp-&gt;datasend("Aucun appel erroné n'a été détecté.\n");
} else {
for (sort @badcalls) {
print "Bad call in line : $_ : $dbtime$_ - $dbroomid$_ - $dbevent$_\n";
$smtp-&gt;datasend("$dbtime$_ - numèro de local : $rooms{$dbroomid$_}\n");
}

# THIS IS WHERE THE ATTACHMENT IS DONE
$smtp-&gt;datasend("--frontier\n");
$smtp-&gt;datasend("Content-Disposition: attachment; filename=\"$csvfile\"\n");
$smtp-&gt;datasend("Content-Type: application/text; name= $csvfile ");
$smtp-&gt;datasend("\n");
open CSVFH, "&lt; $csvfile";
while (&lt;CSVFH&gt;) { chomp; $smtp-&gt;datasend("$_\n"); }
close CSVFH;
}
$smtp-&gt;datasend("--frontier--\n");
$smtp-&gt;dataend();
$smtp-&gt;quit;
}
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
NB : the $_ that appear as links to other nodes should really just be '$_' but in between brackets.
&lt;/p&gt;</field>
</data>
</node>
