Solved

Sending emails with inline attachments Net::SMTP

Posted on 2004-04-12
6
813 Views
Last Modified: 2008-03-17
I need a method to send an email with attachment in Perl without using Mime::Lite.  Every example on the Web that I have found uses Mime:Lite to email attachments, but my installation of ActiveState Perl will not support Mime:Lite.

Can anyone show me a basic method to email attachments using Mime:Lite that would be compatible with ActiveState Perl 5.8?

Thanks!
0
Comment
Question by:gmahler5th
  • 3
  • 2
6 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 10809651
Why do you think ActiveState Perl won't support MIME::Lite?    Of course it will.  MIME::Lite is one of the easiest modules to install.  It consists of a single file which you can place anywhere on your server.

0
 

Author Comment

by:gmahler5th
ID: 10814659
It's currently not installed.  
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10818636
Then just install it.

With ActiveState Perl, you can just do

ppm install MIME::Lite

That's all there is to it.  Easy.

You can even install it manually if you really want to.  It's just as easy.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:gmahler5th
ID: 10826301
Okay, now I'm using Mime:Lite (finally got it installed...)  I'm receiving the email correctly, but it's not sending with an actual attachment.  It is sending in base64.  How can I make the script send an actual attachment?  (Code below)

----------------------
#! e:\perl\bin
use CGI qw(:standard);            
print header();


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


my $from_address = 'steve@stevetout.com';
my $to_address = 'sujit.ravindranath@attws.com';
my $subject = 'MIME Net::SMTP test';
my $mime_type = 'TEXT';
my $message = "Hello world!\n";

# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
   From => $from_address,
   To   => $to_address,
   Subject => $subject,
   Type => $mime_type,
   Data => $message
   )
  or die "Error creating MIME body: $!\n";

my $filename = 'e:\inetpub\wwwroot\sso\upload\test.zip';
my $recommended_filename = 'better_filename.zip';

# Attach the test file
$mime_msg->attach(
   Type => 'application/zip',
   Path => $filename,
   Filename => $recommended_filename
   )
  or die "Error attaching test file: $!\n";

my $message_body = $mime_msg->body_as_string();


# Set this variable to your smtp server name
my $ServerName = "nwestmail.entp.attws.com";

# Create a new SMTP object
$smtp = Net::SMTP->new($ServerName) or
   die "Couldn't connect to server";

$smtp->mail( $from_address );
$smtp->to( $to_address );

$smtp->data();
$smtp->datasend($message_body);
$smtp->dataend();

# Close the connection
$smtp->quit();  

----------------------
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10828789
No need to manually do the SMTP stuff yourself with MIME::Lite.  The problem is that you are missing the essential headers as you are just using the mail body.

Change your script to:

#! e:\perl\bin
use strict;
use CGI qw(:standard);  
use MIME::Lite;
         
print header;

my $from_address = 'steve@stevetout.com';
my $to_address = 'sujit.ravindranath@attws.com';
my $subject = 'MIME Net::SMTP test';
my $mime_type = 'TEXT';
my $message = "Hello world!\n";

# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
   From => $from_address,
   To   => $to_address,
   Subject => $subject,
   Type => $mime_type,
   Data => $message
   )
  or die "Error creating MIME body: $!\n";

my $filename = 'e:\inetpub\wwwroot\sso\upload\test.zip';
my $recommended_filename = 'better_filename.zip';

# Attach the test file
$mime_msg->attach(
   Type => 'application/zip',
   Path => $filename,
   Filename => $recommended_filename
   )
  or die "Error attaching test file: $!\n";

MIME::Lite->send('smtp', "nwestmail.entp.attws.com", Timeout=>60);

$mime_msg->send or die "Problems sending mail $!\n";

0
 

Expert Comment

by:billfinkri
ID: 14536657
TinTin - Thanks!

What a wonderful example - and EASY to follow!

I wasn't even looking for a solution to this sort of problem, stumbled arcoss this thread - and this came in VERY handy!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question