Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::SMTP vs Mail::SendMail

Status
Not open for further replies.

EchoCola

Programmer
Apr 13, 2004
48
US
Hey guys, I'm gonna be writing emails from SMTP virtual server running off of IIS. Currently I'm using Net::SMTP module to write emails but im gonna have to start writing CC: 's. I dont think Net::Smtp supports it, I looked through the code in Net::Smtp and i see some references to it, but not really sure if i can use it.I DONT wanna just use an array/list and just put all the addresses into the To: section. I thought it had to do with MIME headers but it didn't. I wanted to know, if Net::Smtp does in fact support CC and how to implement it. Otherwise I'll have to use Mail::SendMail and try my luck at that. Thanks for your help, it's greatly appreciated!
 
As I understand it, you just add all the email addresses you wan to send to into the "to" parameter, be sure to seperate each address with a comma.

There's always a better way. The fun is trying to find it!
 
Yes, i know of that method, I dont want to do that because when you open it up in an e-mail program say outlook, when you reply they will be all on the To: heading and not in the CC: heading
 
EC,

Suck it and see

what's the worst that could happen?
(think the worst possible, consider the probable, and profit the difference either way)

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Net::SMTP does support the CC option. I use it fairly regularly. Just put the following code in to implement it. This code does assume that you're using an array to store your e-mail addresses in it.

Code:
my $user = "user@website.com";
my @cc;
my @to;
my $mailserver = '0.0.0.127';

my $smtp = Net::SMTP->new("$mailserver", Debug => 0,);

$smtp->mail('postmaster@0.0.0.127');

foreach my $mail (@to)
{
    $smtp->to("$mail");
}
foreach $mail (@cc)
{
    $smtp->cc("$mail");
}

$smtp->data();
$smtp->datasend("From: $user\n");

my $to_addrs;
my $count = 1;

foreach $mail (@to)
{
    $to_addrs = "$mail";
    if ($count != $#to)
    {
        $to_addrs = ", ";
    }
    $count++;
}
my $cc_addrs;
$count = 1;

foreach $mail (@cc)
{
    $cc_addrs = "$mail";
    if ($count != $#cc)
    {
        $cc_addrs = ", ";
    }
    $count++;
}

$smtp->datasend("To: $to_addrs\n");
$smtp->datasend("CC: $cc_addrs\n");

#Continue processing E-mail

- Rieekan
 
When i put an address in @cc i get the following error:

Can't locate object method "cc" via package "Net::SMTP" at C:\PerlStuff\CC.pl line 24.
 
Check your version, and if necessary update it

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Code:
$smtp->mail("from@example.com");
$smtp->recipient("to@domain.com",      "user@example2.com");
 
$smtp->to("user1@domain.com");
$smtp->cc("foo@example.com");
$smtp->bcc("bar@blah.net");


Kind Regards
Duncan
 
Thank you for your prompt replies but I'm still getting the same error messages when i put either, "cc" or "bcc" here is the code, i also updated from libnet 1.18 to 1.19, still, no dice. Here is the code:
Code:
#!C:\apps_pub\ActivePerl\bin
#Script that connects to a remote server using the NET::SMTP module included in this folder
#it then issues SMTP commands to send out e-mails


@recipients = ('me@me.com');
use Net::SMTP;
my $DEBUG = 1;
if($DEBUG){
	$| = 1;
	open (STDERR, ">&STDOUT");
}

my $ServerName = "111.11.111.111";
$smtp = Net::SMTP -> new($ServerName, Debug => 1);
die "Couldn't connect to server" unless $smtp;




print "Please Enter SENDER MAIL ADDRESS: ";		 $sender_mailaddy = mailaddy();
#print "\nPlease Enter RECIPIENT MAIL ADDRESS: ";	 $receipt_mailaddy = mailaddy();
print "\nEnter a SUBJECT: ";				$subject = input_chomper();
print"\n\nNow Type Your MESSAGE: ";			$message= input_chomper();

       

my $MailFrom = "$sender_mailaddy";
my $MailTo = @recipients;


$smtp -> mail( $MailFrom );
$smtp -> recipient(@recipients);
$smtp->to('me@me.com');
#$smtp->cc('me@me.comm');
$smtp->bcc('me@me.com');




$smtp -> data();


$smtp -> datasend ("To: @recipients\n");
$smtp -> datasend ("Cc: @recipients\n");
$smtp -> datasend ("From: $sender_mailaddy\n");
$smtp -> datasend ("Subject: $subject\n");
$smtp -> datasend ("$message\n\n");
$smtp -> dataend();
$smtp -> quit();

##################################( Function Definitions )#############################################
sub mailaddy{
	$mailaddy = <STDIN>;
	chomp($mailaddy);
	while ($mailaddy !~ /^*.@.*\./){
  		print "You entered a bad e-mail address, enter again:  ";
		$mailaddy = <STDIN>;
		chomp($mailaddy); 	
	}
	return $mailaddy;
}
#------------------------------------InputChomper------------------------------------------------------------#
sub input_chomper{
	$line_in = <STDIN>;
	chomp($line_in);
	return $line_in;
}
 
ppm upgrade
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
why would i need to upgrade the ppm if the other parts are working ie

$smtp->to();
 
ppm upgrade will list those modules that you have for which upgrades are available.

If it looks like a duck, acts like a duck, and quacks a lot it's probably not a platypus

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I used PPM and all of my modules are up to date
 
So, what did you do different to make it work?

There's always a better way. The fun is trying to find it!
 
I just put all the people i want to send the mail to in an array and pass it to the recipients function and it will send the mail to all the people. The key however is in the MIME headers all you have to do is instead of putting all the addresses in the To: Header just put the first couple so I did that by using array indexing. @recipients[0] that sends it to the first person and then put the rest in the CC: header and voila, it works.

this is what it looks like
Code:
my $MailFrom = "$sender_mailaddy";
my $MailTo = @recipients;


$smtp -> mail( $MailFrom );
$smtp -> recipient(@recipients);
$smtp -> data();


$smtp -> datasend ("To: @recipients[0]\n");
$smtp -> datasend ("Cc: @cc_receipt\n");
$smtp -> datasend ("From: $sender_mailaddy\n");
$smtp -> datasend ("Subject: $subject\n");
$smtp -> datasend ("$message\n\n");
$smtp -> dataend();
$smtp -> quit();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top