Email Attachment Question: SMTP

A

amerar

Hi All,

I have a Perl script that uses NET::SMTP to send out emails. I've
attached a snippet of my code below.

I want to also attach a file to this. I see lots of posts on using
MIME::LITE, however, I am not understanding how it will work with
NET::SMTP.

Can anyone help, maybe provide some examples?

Thanks in advance, and here is an example of my code:

$smtp = Net::SMTP->new("nitelife");
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend("From: $from1\n");
$smtp->datasend("To: $to1\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Mime-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=us-ascii\n");
$smtp->datasend("Content-Disposition: inline\n");
$smtp->datasend("Content-Transfer-Encoding: 7bit\n");
$smtp->datasend("\n");
$smtp->datasend("<br><br>\n\n");
$smtp->datasend("Text goes here......\n");
$smtp->datasend("Text goes here......\n");
..
..
$smtp->dataend();
$smtp->quit;

Arthur
 
B

Brian McCauley

Hi All,

I have a Perl script that uses NET::SMTP to send out emails. I've
attached a snippet of my code below.

I want to also attach a file to this. I see lots of posts on using
MIME::LITE, however, I am not understanding how it will work with
NET::SMTP.

What is it that you how to achive by explicily using Net::SMTP and
MIME::Lite that you couldn't do just by using MIME::Lite's send()
method to use Net::SMTP (as per the documentation)?
 
T

Tim Heaney

I have a Perl script that uses NET::SMTP to send out emails. I've
attached a snippet of my code below.

I want to also attach a file to this. I see lots of posts on using
MIME::LITE, however, I am not understanding how it will work with
NET::SMTP.

Don't use Net::SMTP yourself directly; just let MIME::Lite use it.

use MIME::Lite;

# Create a new multipart message.
$msg = MIME::Lite->new(To => $to1,
Subject => $subject,
Type =>'multipart/mixed',
);

# Attach the body.
$msg->attach(Type => 'TEXT',
Data => "Text goes here......",
);

# Attach a file.
$msg->attach(Type => 'image/png',
Path => 'granite.png',
);

# Tell MIME::Lite to use Net::SMTP when sending mail, instead of
# its default method.
MIME::Lite->send('smtp', "nitelife");

# Now the behavior of the send method has been altered; it will
# send with Net::SMTP. You don't have to do anything else.
$msg->send;

I hope this helps,

Tim
 
A

amerar

Brain,

What I am unclear on is this.....how do I put multiple lines of text
into the body or DATA section?

My current program uses a FOR loop like this:

foreach $line (@html_letter) {
$smtp->datasend("$line\n");
}

Looking at the documentation from MIME::LITE, it shows this:

# Attach the body.
$msg->attach(Type => 'TEXT',
Data => "Text goes here......",
);

How do I place my lines of text so that they format correctly, etc?
The script has several places where it adds lines to the email message
depending on certain conditions and such.........

I hope I am being somewhat clear in my questions.

Arthur
 
B

Brian McCauley

Brain,

What I am unclear on is this.....how do I put multiple lines of text
into the body or DATA section?

My current program uses a FOR loop like this:

foreach $line (@html_letter) {
$smtp->datasend("$line\n");
}

Looking at the documentation from MIME::LITE, it shows this:

# Attach the body.
$msg->attach(Type => 'TEXT',
Data => "Text goes here......",
);

How do I place my lines of text so that they format correctly, etc?
From reading the docs (untested)...

$msg->attach(Type => 'text/html',
Data => join("\n",@html_letter),
);

If you can arrange that the data in @html_letter already has (or does
not need) newlines added then...


$msg->attach(Type => 'text/html',
Data => \@html_letter,
);
 
P

Paul Lalli

May I also show you this example........I'm curious of your opinions:

Who is "you"? Why are you showing us this example? Please quote some
context when posting a reply to Usenet.

That posting is more than 7 years old. MIME::Lite has involved a lot
since then. Most specifically in this case, about a month after this
posting you referenced, MIME::Lite was changed to automatically use
Net::SMTP rather than relying on `sendmail`. See
http://search.cpan.org/~yves/MIME-Lite-3.01/changes.pod

Read the documentation for the module you want to use:
http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm

Paul Lalli
 
U

usenet

Hi All,

I have a Perl script that uses NET::SMTP to send out emails. I've
attached a snippet of my code below.
<snipped snippet>

You are going to a lot of trouble there, my friend. Consider
Mail::Sender instead:

#!/usr/bin/perl
use strict; use warnings;

use Mail::Sender;
(new Mail::Sender)->MailFile({
'smtp' => 'mail.mySMTPserver.com',
'from' => '(e-mail address removed)',
'to' => 'nobody@no_such_domain.com',
'subject' => 'Something for you',
'msg' => 'Here is a file for you.',
'file' => '/path/to/some/file.mp3'
}) || die "Cannot send message: $!\n";

__END__
 
A

amerar

David,

What would the MAIL::SENDER look like if I was sending an HTML message,
with inline images, AND an attachment? Do I not have to show the type
of attachment it is, like text/plain or image/gif?

Arthur
 
A

amerar

Also, a good thing about NET::SMTP is that I can use 2 different mail
hosts : $smtp = Net::SMTP->new("$mailhost");

Can I do that with MAIL::SENDER??
 
G

Gunnar Hjalmarsson

Consider Mail::Sender instead:

#!/usr/bin/perl
use strict; use warnings;

use Mail::Sender;
(new Mail::Sender)->MailFile({
'smtp' => 'mail.mySMTPserver.com',
'from' => '(e-mail address removed)',
'to' => 'nobody@no_such_domain.com',
'subject' => 'Something for you',
'msg' => 'Here is a file for you.',
'file' => '/path/to/some/file.mp3'
}) || die "Cannot send message: $!\n";

I like Mail::Sender, too, but the above error check is not correct. Try
this instead:

ref ( new Mail::Sender->MailFile({
'smtp' => 'mail.mySMTPserver.com',
'from' => '(e-mail address removed)',
'to' => 'nobody@no_such_domain.com',
'subject' => 'Something for you',
'msg' => 'Here is a file for you.',
'file' => '/path/to/some/file.mp3'
}) ) || die "Cannot send message: $Mail::Sender::Error\n";

Think it's described in the docs, btw. ;-)
 
U

usenet

Also, a good thing about NET::SMTP is that I can use 2 different mail hosts

Can I do that with MAIL::SENDER??

It isn't documented, but I tried this:

my @smtp_servers = qw/mail.totally_bogus_server.com
mail.myserver.com/;

and then passed it (by reference) as a hash value:

'smtp' => \@smtp_servers,

and it skipped the bogus server and went to my real mailserver.

So apparently it does work...
 
U

usenet

It isn't documented, but I tried this:

my @smtp_servers = qw/mail.bogus_server.com mail.myserver.com/;

and then passed it (by reference) as a hash value:

'smtp' => \@smtp_servers,

and it skipped the bogus server and went to my real mailserver.

So apparently it does work...

I'd like to 'retract' this post; I, ummm, didn't, ummm, actually save
the script once I made the testing change that I described... D'OH!

When I actually SAVED the change, it failed. So, AFAIK, Mail::Mailer
won't allow you to specifiy more than one SMTP server for failover, as
the Net module does.

Sorry for the incorrect info...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top