Send a file by email

software development


I’m looking for a straightforward Perl script to send a file (Word doc or PDF) to a user by email when they answer a bunch of questions correctly. My client wants to do it this way rather than by making the file downloadable as they don’t want every Tom, Dick and Harry bombarding them with applications, only those who meet certain criteria. Their site is using straight HTML and I don’t want to get into PHP at this time.

Been banging my head on this all day.

Ideas, pointers welcome.



OK, I’ve got something going, but DreamHost is munging the headers badly. Here’s the code:

#!/usr/bin/perl -w

use MIME::Lite;
use Net::SMTP;

Adjust sender, recipient and your SMTP mailhost

my $from_address = ‘username@domain.ext (Human Friendly Name)’;
my $to_address = ‘“My Name” <myname@mydomain.ext>’;
my $mail_host = ‘mail.domain.ext’;

Adjust subject and body message

my $subject = ‘2010 Grant Application’;
my $message_body = “Here’s the 2010 Grant Application Form. Fill it out and email it back to the address in the included instructions.”;

Adjust the filenames

my $my_file = ‘/home/dhusername/webroot/path/file-to-send.doc’;
my $your_file = ‘file-to-send.doc’;

Create the multipart container

$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>‘multipart/mixed’
) or die “Error creating multipart container: $!\n”;

Add the text message part

$msg->attach (
Type => ‘TEXT’,
Data => $message_body
) or die “Error adding the text message part: $!\n”;

Add the GIF file

$msg->attach (
Type => ‘application/ms-word’,
Path => $my_file,
Filename => $your_file,
Disposition => ‘attachment’
) or die “Error adding $your_file: $!\n”;

Send the Message


The message goes out and I receive it with the attachment, but when I get it, the sender and receiver names in the message header are badly munged. Here they are:

From: “username@domain.ext” (Human Friendly Name)
To: “My Name” <“myemail@mydomain.ext”>

DH is appending "" to both the $from_address and the $to_address but just inside message, not in the control files, as the mail is getting through.

Anyone have ANY idea how I can fix this? This looks horrible.


You need to set $from_address to a valid email address - the format you’re using is invalid, and is getting reformatted by the mail server into something slightly more correct. You have the correct format on $to_address already, so just use the same format for the source and it’ll come out properly.


Still doesn’t do it.

Here’s the revised code:

------- Included Stuff Follows -------

Adjust sender, recipient and your SMTP mailhost

my $from_address = ‘“My Name” <myaddress@domain.ext>’;
my $to_address = ‘“Another Name” another@domain2.ext’;

--------- Included Stuff Ends ---------

Here are the new received headers:

------- Included Stuff Follows -------

Date: Fri, 4 Dec 2009 20:40:59 -0800
From: "“My Name” <“myaddress@domain.ext”>
To: “Another Name” <“another@domain2.ext”>

--------- Included Stuff Ends ---------

Question: if the $to_address is formatted correctly, why is IT also