LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to send email message (https://www.linuxquestions.org/questions/programming-9/script-to-send-email-message-51699/)

meluser 03-25-2003 02:31 PM

script to send email message
 
Hi,

I have been researching on this. i got a script when i try to run get an error connecting to server. This script i am just trying to get to work, but the main reason for this is to be able to insert it into another Perl script where i would like email to be sent automatically when the other part of the script does something!

[root@cctv cgi-bin]# perl email.pl
Content-type: text/plain

Couldn't connect to server at email.pl line 22.
[root@cctv cgi-bin]#


Regards,

Mel

---------------------email.pl

#!/usr/local/bin/perl

use Net::SMTP;

print "Content-type: text/plain", "\n\n";

my $DEBUG = 1;

if($DEBUG)
{
$| = 1;
open(STDERR, ">&STDOUT");
}

# Set this variable to your smtp server name
my $ServerName = "smtp.134.36.16.12";

# Create a new SMTP object
$smtp = Net::SMTP->new($ServerName, Debug => 1);

# If you can't connect, don't proceed with the rest of the script
die "Couldn't connect to server" unless $smtp;

# Initiate the mail transaction
# Your "real" email address
my $MailFrom = "cctv_server\@hotmail.com";

# Recipient's "real" email address
my $MailTo = "cctv_alert\@hotmail.com";

$smtp->mail( $MailFrom );
$smtp->to( $MailTo );

# Start the mail
$smtp->data();

# Send the header
# This address will appear in the message
$smtp->datasend("To: cctv_alert\@hotmail.com\n");

# So will this one
$smtp->datasend("From: cctv_server\@hotmail.com\n");
$smtp->datasend("Subject: Test Message\n");
$smtp->datasend("\n");

# Send the body.
$smtp->datasend("Hello World!\n\n");

# Send the termination string
$smtp->dataend();

# Close the connection
$smtp->quit();

david_ross 03-25-2003 02:59 PM

Personally I would use sendmail but I think the problem is that the line
my $ServerName = "smtp.134.36.16.12";
should be
my $ServerName = "134.36.16.12";

j-ray 03-26-2003 12:35 AM

add this and u will probably get a more detailed error message.

# If you can't connect, don't proceed with the rest of the script
die "Couldn't connect to server: $!" unless $smtp;

does the server require authentication?
cheers, jens


All times are GMT -5. The time now is 03:06 PM.