Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sending emails with inline attachments Net::SMTP

Posted on 2004-04-12
6
Medium Priority
?
834 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 1500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

782 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