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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net:SMTP - cc & bcc not working? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Why does the CPAN doc along with many other web articles say you can use cc and bcc with Net:SMTP like so

Code:
 $smtp->cc('email@domain.com');
 $smtp->bcc('email@domain.com');
but when i do i get an error...
Code:
Can't locate object method "cc" via package "Net::SMTP"
Can't locate object method "bcc" via package "Net::SMTP"

does cc & bcc exist or not and if so what wrong with my syntax, because doing ...
Code:
 $smtp->to('email@domain.com');

works fine for a standard TO address. so why should cc & bcc not be working? any ideas.
 
Had a mare with that also, so I use MIME::Lite and all worked fine.

Works well for attachments also.


my $msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Cc => $from_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 ZIP file
$msg->attach (
Type => 'application/zip',
Path => $my_file_zip,
Filename => $your_file_zip,
Disposition => 'attachment'
) or die "Error adding $your_file_zip: $!\n";
 
so basically it was a waste of time investigating and programing with Net::SMTP. because it doesn't work and anything CPAN says should be ignored because you can't trust if what they say is correct or not.

Great how's a guy suppose to develop when people keep going "Refer to CPAN" , "R.T.F.M." if when I do they aren't worth the kilobytes of ram it takes to load the page!

I'm reluctant to re-write my whole email routine and use another module for the sake of it, so if anyone can help rectify this problem i would be very grateful.

However if not and everyone confirms your view that Net::SMTP doesn't work, I thank you for your code and hope it won't take me long to re-write.

I was going to use MIME::Lite, but my application didn't require attachments so didn't bother.

Isn't PERL fun!
 
Wow - someone sounds a little bitter. You must be right though, because you had some confusion with the documentation for one module, you should just disregard everything else on the CPAN. It must all be wrong. You are joking, right?

I do notice when looking at the Net::SMTP documentation it does say that to, cc and bcc are synonyms for recipient. I read that as everyone ending up in what will be the "To" field of the email. Having not used the module before, I can't say for sure.

If using the BCC field is required, you could send out multiple emails with the same message, with one recipient per email.
 
Personally I use Mail::Sendmail it supports cc and bcc just fine with a very small foot print. I figured using Mime::Lite was akin to hunting grasshoppers with machine guns.

I guess I'll have to try Net::SMTP to see what the fuss is about.

All of CPAN is junk? I think their might be a youngster amongst us.

Ed
 
Looking over the Net::SMTP module, the functions for To, CC, and BCC do in fact just append the e-mail addresses in the To field of the e-mail and are just synonyms of the recipient routine. As a result, even if they were working on your platform (I've never had a problem using them in the past), you'd see all of the e-mail addresses in the To field of the e-mail.

One easy way to work around that is to send everything to everyone, but add in the following line. It takes the e-mail addresses away from being shown and allows you to put whatever you'd like there, even a list of the true To recipients. (I've used this in the past when the Tooth Fairy would remind co-workers that they had a dentist appointment. heh)

Code:
$smtp->datasend("To: toothfairy@toothfairy.net\n");

You can use multiple addresses there if you'd like, it doesn't matter one bit.

Don't get discouraged just because one little bit of code isn't working 100% for you. Everything is a work in progress.

- Rieekan
 
Yes i'm bitter about this - sorry!

It was late afternoon - I'm doing my best to re-write my scripts properly, the one i was working on was finished apart from adding the bcc recipient and now I find I've got to re-write my whole script again because this Net::SMTP module doesn't work properly.

Do you really blame me for being peeved about this.

Not to mention now you are saying even if the bcc worked it put the email address in the TO field. - you what ? that's not BCC then is it.

OK - i'll just go away quitely and re-write using another module. But a warning to anyone else out there DO NOT USE Net:SMTP - it doesn't work properly and take CPAN with a pinch of salt.

Personally I think CPAN sucks - utterly and completely - I've learnt more from the kind folk here on Tek-Tips than I ever have or will from CPAN.

So when I say CPAN sucks - that's because in general I get better, more informative, easy to understand explanations and SYNOPSIS from Tek-Tips than any where else on the web.

So don't get too upset I was actually paying you guys a compliment.
 
Oh by the way Mime::Lite & Mail::Sendmail are not part of the standard modules that ship with PERL. I only want to use generic modules for my scripts, thats why i use Win32::ODBC and not DBI::DBD.

Is there another way to send mail with To, Cc and Bcc working with a standard PERL module, or is Net the only one.

thanks 1DMF.
 
There's no reason to move to a third module to create what you're looking to. As I stated in my reply, you can handle the CC and BCC e-mail addresses by still sending to everyone using the $smtp-to() function, but adding into the datasend the three separate sections for To, CC, and BCC.

Code:
my $mailserver = 'mail.domain.com';
my @send_list;
my $to = 'bob@bob.com';
my $cc = 'jane@jane.com';
my $bcc = 'tom@tom.com;
my $subject = 'Test subject';
my $message = 'This is a test message to make sure when CCs and BCCs are sent, they are only shown to the people that need to see it.';

push @send_list, $to, $cc, $bcc;

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

$smtp->mail('$from');

foreach $recipient (@send_list)
{
    $smtp->recipient("$recipient");
}

$smtp->data();
$smtp->datasend("From: Website <website\@domain.com>\n");
$smtp->datasend("To: $to\n");
$smtp->datasend("CC: $cc\n");
$smtp->datasend("BCC: $bcc\n");
$smtp->datasend("Reply-To: $email_id\n");
$smtp->datasend("Subject: $subject\n\n");
$smtp->datasend("$message\n");
$smtp->quit();

- Rieekan
 
Hello Rieekan,

I appreciate the post but i'm not sure you understand what Bcc means. (Blind Carbon Copy).

I'm staying with the module as hiding the second recipient for my purpose isn't that big a deal, but it's now an issue for some of my application.

It's a shame it doesn't work properly, i mean you can do the $smtp->datasend("Cc: $email_address\n");

and it shows when recieving the email in the header - but it doesn't actually send them a copy.

all email adressess passed via $smtp->recipient(@email_address); are shown in the TO field!

Another one of those annoyances courtesy of PERL.

Oh well got to much to be getting on with to worry about it anymore!
 
Run the code I sent you. It does exactly what you're looking for.

And so you know, I do know what BCC stands for, and was just trying to help you from having to write your code again. Let's stop being rude on here when people try to help you with things you're working on.

- Rieekan
 
I'm confused I have done exactly that and it didn't work - all emails showed in the TO column - what platform you running on ?

I was not meaning to be rude I was making a statement based on my opinion judged by the fact your code doesn't work.

If that offended you then please accept my appologies.
 
You have to be sure to put in the lines:

Code:
$smtp->datasend("To: $to\n");
$smtp->datasend("CC: $cc\n");
$smtp->datasend("BCC: $bcc\n");

This is where your e-mail reader is told who receives this as a To, CC or BCC. If a user is in the BCC section here, they will not appear in any e-mail or reply, but still receive the message.

I've tested my script above on both a Windows 2000 server and Linux box with Perl 5.8. I actually use this method very frequently to send messages that I want to have sent blindly to my own account for testing.

- Rieekan
 
I've tried every permatation I've got as far as the names not appearing on the TO line but the Bcc don't get sent an email - i've no idea what else I can do.

Here's my code.

Code:
##############################
###### SENDMAIL ROUTINE ######
##############################

sub send_mail {

#############################
# Use Net::SMTP For mailing #
#############################

use Net::SMTP;

#_[0] = To
#_[1] = From
#_[2] = Subject
#_[3] = Body Text
#_[4] = Bcc


# Create new instance of SMTP
my $smtp = Net::SMTP->new(SMTP_DOMAIN, Hello => SMTP_DOMAIN) || die "Can't open mail connection: $!";

# convert recipients to array from CSV
my @recip = split(/;/, $_[0]);
my @bcc = split(/;/, $_[4]);

$smtp->mail($_[1]);

# send recipient TO data
foreach my $dt (@recip){
    $smtp->recipient($dt);
}

# send recipient BCC data
foreach my $dt (@bcc){
    $smtp->recipient($dt);
}

# Send Mail
$smtp->data();
$smtp->datasend("To: $_[0]\n");
$smtp->datasend("Bcc: $_[4]\n");
$smtp->datasend("From: $_[1]\n");
$smtp->datasend("Subject: $_[2]\n");
$smtp->datasend("Content-type: text/html\n\n");
$smtp->datasend("$_[3]\n");
$smtp->dataend();
$smtp->quit();

}
 
Code:
#!/bin/perl -w

use strict;
use warnings;

use Net::SMTP;
my $mailserver = 'mail.domain.cmo';
my @send_list;
my $from = 'mail@domain.com';
my @to = ('to@domain.com','to@domain2.com');
my @bcc = ('bcc@domain.com','bcc@domain2.com');
my $subject = "Test subject";
my $message = "This is a test message to make sure when CCs and BCCs are sent, they are only shown to the people that need to see it.";


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

$smtp->mail($from);

$smtp->recipient(@to);
$smtp->bcc(@bcc);

$smtp->data();
$smtp->datasend("From: Website <website\@domain.com>\n");
$smtp->datasend("To: @to\n");
#$smtp->datasend("CC: $cc\n");
$smtp->datasend("BCC: @bcc\n");
#$smtp->datasend("Reply-To: $email_id\n");
$smtp->datasend("Subject: $subject\n\n");
$smtp->datasend("$message\n");
$smtp->quit();

well the above works for me. I would advice to reinstall the net::smtp module since you are getting errors using smtp->bcc().

the above results in 4 mails with only the email addresses in the to field appearing.

Another thing.. You are splitting an array and putting every string in the to and bcc field seperatly. Just put in the array itself.

InDenial

 
Hello InDenial.

Thanks for the relpy, only I cant re-install a module on our web host, once again it would seem I have found another reason to move.

The splitting array thing, I know, I've passed it the array, i've split it out, i've used a single scalar...NOTHING WORKS!

I wish I knew why, oh well what can you do,

regards,

1DMF

 
I'm stumped, here's the scenario I have bob & sue who are in the To: field, steve & jack are in the Bcc: field.

bob & sue get the email, steve and jack do NOT. although steve & jack do not show as expected in the emails that are recieved as they are Bcc, they just don't get the email.

here's my code...($body is HTML of the email)

Code:
&send_mail(
"bob\@domain.com,sue\@domain.com","courses\@domain.com",
"Course Booking Confirmation","$body",
"steve\@domain.com,jack\@domain.com
);

the sendmail routine is as follows...
Code:
##############################
###### SENDMAIL ROUTINE ######
##############################

sub send_mail {

#############################
# Use Net::SMTP For mailing #
#############################

use Net::SMTP;

#_[0] = To
#_[1] = From
#_[2] = Subject
#_[3] = Body Text
#_[4] = Bcc

my (@Bcc, @Eadds, @recip);

# Create new instance of SMTP
my $smtp = Net::SMTP->new(SMTP_DOMAIN, Hello => SMTP_DOMAIN) || die "Can't open mail connection: $!";

# convert recipients to array from CSV
@recip = split(/\,/, $_[0]);

# add addressess together
push @Eadds, @recip;

# Check for Bcc
if($_[4] ne ""){
    @Bcc = split(/\,/, $_[4]);
    push @Eadds, @Bcc;
}

# Send Mail
$smtp->mail($_[1]);
$smtp->recipient(@Eadds);
$smtp->data();
$smtp->datasend("To: @recip\n");
$smtp->datasend("From: $_[1]\n");

if($_[4] ne ""){
    $smtp->datasend("Bcc: @Bcc\n");
}

$smtp->datasend("Subject: $_[2]\n");
$smtp->datasend("Content-type: text/html\n\n");
$smtp->datasend("$_[3]\n");
$smtp->dataend();
$smtp->quit();

}
 
1DMF.

turn on warnings and strict and run the script again. You will see that there and where something is wrong.

After I made the correction the script works. I have a few remarks though:

In the calling of the sub send_mail the $body is between "". Although it works with quotes I am not sure if there should be "". (It works without quotes too)

The result of putting $smtp->datasend("Bcc: @Bcc\n"); in the there is unwanted. As far as I can see.. the only thing that the line does is showing the bcc addresses in the header of the mail. Just take away the line.

InDenial

 
Hello InDenial,

1. I always use (use strict & warnings) along with use CGI::Carp qw(fatalsToBrowser);

2. I always surround a variable that holds text in quotes and it's never failed - i thought that was the correct syntax for text values and no quotes for numeric the same way you would then use != or == for numberic and NE or EQ for text.

3. I think your right about the header comment, but it was worth a try as $smtp->Bcc(@bcc); does not work with the error at the top of this thread.

Like I say I'm just going to have to give up until I move host, either they don't have the module installed properly or they deliberately block BCC email (if thats possible).

However $smtp->cc(@cc) doesn't work either so who knows what they have done.

Regards,

1DMF
 
Well I thought you didn't use strict and warnings since I got an error when I tried your script. In the part where you call the send_mail sub you forgot a " at the last string wich is the reason why those two persons did not get their emails...

Also.. running the script using debug => 1 might give you some info on where it goes wrong..

I you still are not able to get it working I hope you will have better luck with another module...:)



InDenial

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top