CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Specifying Subject: and To: fields with Net::SMTP

 



kencl
User

Feb 14, 2000, 3:07 PM

Post #1 of 12 (1911 views)
Specifying Subject: and To: fields with Net::SMTP Can't Post

I've managed to get Net::SMTP to send e-mail to the appropriate address, but I can't seem to figure out how to specify either the Subject: field or the To: field.

Odd about the To: field quirk, since the e-mail does go where it's supposed to. You'd think it would just insert the value of recipient into this field.

Thanks.


perlkid
stranger

Feb 15, 2000, 2:16 PM

Post #2 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

 
Post the program that you are using.


kencl
User

Feb 15, 2000, 4:19 PM

Post #3 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

sub SendMail {
$WhoGetsIt = $_[0];
@Message = @_;
$smtp = Net::SMTP->new('mail.pinc.com') | | &dienice("Unable to establish a connection with mail.pinc.com.");
$smtp -> mail("walkermailinglist\@walkersys.com") | | &dienice("mail didn't work");
$smtp -> recipient($WhoGetsIt) | | &dienice("recipient didn't work");
$smtp -> data(@Message) | | &dienice ("data didn't work");
$smtp -> quit | | &dienice("Connection didn't quit.");
}


perlkid
stranger

Feb 15, 2000, 4:52 PM

Post #4 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

  
I have never seen that method of sending email before. I would use somthing more like this. Are you on an NT server? What I did here is after parsing the form's input and storing it into a hash I printed the subject and the reply to email adress. I also have the program check for a few things in the email field which is the text before the @ symbol the text after it and the .(dot)extention.


{CODE}--------------------------------------------------
#!/usr/bin/perl

$yourmail="yourname\@yourdomain.com";

# Default URL to redirect to after a sent message:
$yourredir='http://www.site.com/thanks.html';

# Url to send the user to if they don't access the page using a POST
$redd='http://www.form_url';

# Sort method ( 1 = The order of the fields on the original form, 2 = Alphabetically, 3 = random :-)
$sort=1;

# Path to sendmail:
$mailprog="/usr/lib/sendmail";

&parse_form;

$mailto=$FORM{'to'};
$redir=$FORM{'redir'};
$red=$ENV{'HTTP_REFERRER'};
$red="$redd" unless ($red);


#The default recepient of the mail.
$mailto="$yourmail" unless ($mailto);
$redir="$yourredir" unless ($redir);

$method=$ENV{'REQUEST_METHOD'};
if ($method !~ /POST/i) {
#The script is not receiving POST. Go back to the form.
print "Location: $red\n\n";
exit;
}


#Essential parts of the form.
$name=$FORM{'name'};
$email=$FORM{'email'};
$phone=$FORM{'phone'};
$subject=$FORM{'subject'};
$body=$FORM{'body'};

if ($email =~ /.+@.+\..+/)
{
}
else
{
print "Content-type:text/html\n\n";
print "<p>&nbsp</p>";
print "<p>&nbsp</p>";
print "<p align = center><font face = verdana size=2>The email address: $FORM{'email'} doesn't look like a real email address. Please use your back button and check your email address.</font></p>";
exit;
}

@keys=(keys %FORM);
if ($sort==1) {@keys=@names;}
elsif ($sort==2) {@keys=sort @keys;}

open (MAIL, "| $mailprog -t -oi") &#0124; &#0124; die "Can't open $mailprog";
print MAIL "To: $mailto\n";
print MAIL "Reply-to: $email\n";
print MAIL "From: $email\n";
print MAIL "Subject: $subject\n";
print MAIL "Message:\n\n";
foreach $k (@keys) {
print MAIL "$k: $FORM{$k}\n\n";
}

close (MAIL);

#Redirect to the Thanks page.
print "Location: $redir\n\n";

exit;

sub parse_form {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/[\;\|\\ ]/ /ig;
push(@values,$value); push(@names,$name);
$FORM{$name} = $value;
}
}
{CODE}------------------------------------------------

If you are on NT this should still work. But to use what you already have I would take a few things from the above script. Like the

if ($email =~ /.+@.+\..+/)

condition to check for a proper email adress, and I would take the bit of code that print the subject from the form parse.

Hope this helped, Feel free to follow up on this.

perlkid



perlkid
stranger

Feb 15, 2000, 5:11 PM

Post #5 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

 Ok,
I made a little mistake. It will work the way it is but it's better if you were to put everything from the

@keys=(keys %FORM);

to the close(MAIL);

inside of the if brackets,

if ($email =~ /.+@.+\..+/)
{
the code
}

The perl flows better that way.
I also have just checked this with the change and it works like a charm.

Sorry about that,

Perlkid

[This message has been edited by perlkid (edited 02-15-2000).]


darian
Deleted

Feb 16, 2000, 6:22 AM

Post #6 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

This is what I did kencl:

use Net::SMTP;
require 'cgi-lib.pl';

print "Content-type: text/html", "\n\n";
&ReadParse(*input);

$smtp_server = "smtp.your-server.com";
$domain_name = "your-domain.com";

@to = $input{'to'};
$from = $input{'from'};
$subject = $input{'subject'};
$body = $input{'body'};

#--------Connect to the server----------#
$smtp = Net::SMTP->new($smtp_server,
Hello => $domain_name,
Timeout => 30,
Debug => 1,
);


$smtp->mail($from);
$smtp->to('$to');
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("$body\n");
$smtp->dataend();
}


$smtp->quit;

print qq~
TO: $to
FROM: $from
SUBJECT: $subject

$body
~;
exit;

I havn't tested it fully but it should work. I think what was wrong with yours is you did not use the datasend. When you do just data it prepares to send the information. This is of course if I read the docs right. Hope this works for you. I will be testing this out later today I hope.


darian
Deleted

Feb 16, 2000, 6:26 AM

Post #7 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

Just a note for perlkid. Your version send using the program sendmail where as kencl is wanting to send directly to the smtp server. Not all hosts utilize sendmail (would be really nice if they did).


Cure
User

Feb 16, 2000, 8:03 AM

Post #8 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

Hi Kencl

NOTE: I would use CGI.pm to parse your info.

Cure


[This message has been edited by Cure (edited 02-16-2000).]


Cure
User

Feb 16, 2000, 9:38 AM

Post #9 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

Hi:

Heres the code with the use of CGI.pm

#!/usr/bin/perl
use CGI;
use Net::SMTP;
$q= new CGI;



$smtp_server = "smtp.your-server.com";
$domain_name = "your-domain.com";

$to = $q->param->('to');
$from = $q->param('from');
$subject = $q->param('subject');
$body = $q->param('body');

#--------Connect to the server----------#
$smtp = Net::SMTP->new($smtp_server,
Hello => $domain_name,
Timeout => 30,
Debug => 1,
);

$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("$body\n");
$smtp->dataend();
$smtp->quit;

print $q->header;
print qq~
TO: $to
FROM: $from
SUBJECT: $subject

$body
~;
exit;


Cure

[This message has been edited by Cure (edited 02-16-2000).]


perlkid
stranger

Feb 16, 2000, 12:42 PM

Post #10 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

 
oops,

Sory about that kencl,

Don't listen to me.

perlkid


kencl
User

Feb 16, 2000, 3:20 PM

Post #11 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

Success! - Thanks for all the input. Didn't need CGI.pm.

Darian, could you elaborate? "Not all hosts utilize sendmail (would be really nice if they did)." What are the pros/cons?

PerlKid - Linux/Apache/mod-perl

...And here it is:

#!/usr/bin/perl
print "Content-type: text/html\n\n";

## * $From in this example has no effect on the SENDING of the message, but keep in
## mind that users will REPLY to whatever you put here.
## * The To: CC: From: and Subject: fields just make the message look normal
## but have no bearing on where it is actually sent - $WhoGetsIt determines that.

use Net::SMTP;

$theMessage = "Hey, this is working\!\nyeeha\num, yeah, that could be a little more enthusiastic eh\!";
&SendMessage("webmaster\@walkersys.com", $theMessage);

sub SendMessage {
$WhoGetsIt = $_[0]; $Message = $_[1]; $From = "walkermailinglist\@walkersys.com";
$smtp = Net::SMTP->new('mail.pinc.com') &#0124; &#0124; &dienice("Unable to establish a connection with mail.pinc.com.");
$smtp -> mail("$From") &#0124; &#0124; &dienice("mail didn't work");
$smtp -> recipient($WhoGetsIt) &#0124; &#0124; &dienice("recipient didn't work");
$smtp -> data() &#0124; &#0124; &dienice("Unable to open data pipe.");
$smtp -> datasend("To: $WhoGetsIt\n") &#0124; &#0124; &dienice("To: didn't work.");
$smtp -> datasend("CC: Decoration.\n") &#0124; &#0124; &dienice("CC: didn't work.");
$smtp -> datasend("From: $From\n") &#0124; &#0124; &dienice("From: didn't work.");
$smtp -> datasend("Subject: Something appropriate\n") &#0124; &#0124; &dienice("Subject: didn't work.");
$smtp -> datasend("$Message\n") &#0124; &#0124; &dienice("Message didn't work.");
$smtp -> dataend() &#0124; &#0124; &dienice("Dataend didn't work.");
$smtp -> quit &#0124; &#0124; &dienice("Connection didn't quit.");
}

sub dienice {
($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
--------- end ---------

Anyone know how I can change the Content-Type so that I can send HTML e-mail?

Well, off to play with Net::POP3... Smile



darian
Deleted

Feb 16, 2000, 9:41 PM

Post #12 of 12 (1911 views)
Re: Specifying Subject: and To: fields with Net::SMTP [In reply to] Can't Post

kencl,

Sendmail is a program an ISP can install to access the SMTP protocal. As you can see by perlkid's example, there is much less code involved then the example I have below. This example here uses sockets to communicate to the server to send mail via SMTP.

SMTP Code:
#!/usr/local/bin/perl
####################################################################
# This smtp socket script was created by Johnny Hughes 4/25/98 #
# #
# http://ntperling.hypermart.net #
# #
# the below variables can come from a form...or be predefined... #
# or any combination thereof, as long as they are all defined #
# before the needed in the call below. This script is designed to #
# be place inside another script, and is not fully fuctional by #
# itself #
####################################################################
#$emailfrom = 'abc@123.net';
#$recipient = 'def@123.net';
#$subject = 'This is a test mail';
#$message = 'This is a test message...did I get it';
$smtp_server = "mail.yourserver.com";

&email;

sub email {
($x,$x,$x,$x, $here) = gethostbyname($null);
($x,$x,$x,$x, $there) = gethostbyname($smtp_server);
$thisserver = pack('S n a4 x8',2,0,$here);
$remoteserver = pack('S n a4 x8',2,25,$there);
#NOTE, if Solaris, uncomment the line below and delete the one below it...leave alone for NT
#(!(socket(S,2,2,6))) && (&error("Connect error!"));
(!(socket(S,2,1,6))) && (&error("Connect error! socket"));
(!(bind(S,$thisserver))) && (&error("Connect error! bind"));
(!(connect(S,$remoteserver))) && (&error("!! connection to $smtp_server has failed!"));

select(S);
$| = 1;
select(STDOUT);

$DATA_IN = <S>;
($DATA_IN !~ /^220/) && (&error("data in Connect error - 220"));

print S "HELO $ENV{REMOTE_HOST}\r\n";
$DATA_IN = <S>;
($DATA_IN !~ /^250/) && (&error("data in Connect error - 250"));

print S "MAIL FROM:<$emailfrom>\n";
$DATA_IN = <S>;
($DATA_IN !~ /^250/) && (&error("'From' address not valid"));

print S "RCPT TO:<$recipient>\n";
$DATA_IN = <S>;
($DATA_IN !~ /^250/) && (&error("'Recipient' address not valid"));

print S "DATA\n";
$DATA_IN = <S>;
($DATA_IN !~ /^354/) && (&error("Message send failed - 354"));

print S <<MESSAGES;
From: $emailfrom
To: $recipient
Subject: $subject

$message
.
MESSAGES
$DATA_IN = <S>;
($DATA_IN !~ /^250/) && (&error("Message send failed - try again - 250"));

print S "QUIT\n";
print "Content-type: text/html\n\n";
print "<br>Email sucessfully sent\n";
}

sub error {
# Displays any errors and prints out FORM and ENV info.
print "Content-type: text/html\n\n";
print "<PRE>\nCGI Error: $!\n";
print "Message: $_[0]\n\n";
print "\n Form Variables \n";
foreach $key (sort keys %in) {
print "$key: \t$in{$key}\n";
}
print "\n Environment Variables \n";
foreach $env (sort keys %ENV) {
print "$env: \t$ENV{$env}\n";
}
print "\n</PRE>";
exit;
}


Now with the module Net::SMTP you need only to place lines of code to access the subs in the module just like those who are able to use sendmail. The advantage of sending directly through sockets I believe would be speed. Your program would not have to go through another program(i.e. sendmail) to get your email sent. However as you can see there is more code to be written. Now I am in no way an expert on this so don't take what I have said as law. I may be wrong or only partially right. Still learning all this myself.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives