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

Net::SMTP to send mail from perl through outlook

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
i am stumped and don't know enough about what i am doing to know where to go from here.

i am programming a perl cgi script on an intranet site (windows server). part of the script's function is to email form information through our outlook system.

i am used to sendmail, but i have learned it won't work in my situation. plus i will eventually want to send attachments.

so i have been toying with Net::SMTP, but can't get that to work either. here's what i have:

Code:
$to = "me\@domain.com";
$from = "me\@domain.com";
$subject= "Test of PERL script to send email notice";
$body = "This is the body of the test message";

&send_mail($to, $from, $subject, $body);

sub send_mail {

my($to, $from, $subject, $body) = @_;

use Net::SMTP;

my $relay = "domain.com";
my $smtp = Net::SMTP->new($relay) 
    || die "Can't open mail connection: $!";

    $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("\n");

    $smtp->datasend("$body\n"); 

    $smtp->dataend();
    $smtp->quit();
}

i use valid emails for the 'to' and 'from' and change 'domain.com' to the common company email extension, but when i process i get two errors:


Can't open mail connection: domain.com
Can't call method "mail" on an undefined value at line 264


please help if you can.
thanks for your time.

 
Here a good one using Win:;32

It works very good!

use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Outlook';
$Win32::OLE::Warn = 3; # die on errors...


my $objOutlook = Win32::OLE->GetActiveObject('Outlook.Application') ||
Win32::OLE->new('Outlook.Application', 'Quit');


my $objOutlookMsg = $objOutlook->CreateItem(olMailItem);


$objOutlookMsg->{'To'} = "dmazzini\@mydomain.com";
$objOutlookMsg->{'Subject'} = "Test";
$objOutlookMsg->{'Body'} = "This is a test";


# Add thew attachment "Source", Type, Position, 'Display Name'
# Display name must match file name - don't forget to escape the "\"
$objOutlookMsg->{'Attachments'}->Add("C:\\test.xls", olByValue, 1,
'mynewfilename.xls');


$objOutlookMsg->Display();

dmazzini
GSM System and Telecomm Consultant

 
You can also look into using Mail::Sender

I have been using it and it works pretty good.

One thing I noticed about your code was that there is no login information. If your mail server requires authentication, then that could be your problem if you are not supplying the info.


Michael Libeson
 
i tried Mail::Outlook. that module isn't loaded on my server.

btw, i am in a super down-sized environment and i pretty much go it alone with programming so if it ain't loaded it ain't gonna' be.

anyway, can you tell me more about authentication and what i need / how to code it??

i am going to try win::32 next


thanks.
 
Win::32 seems a good direction.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
... and change 'domain.com' to the common company email extension...
What do you mean by the 'company email extension'? You should be putting in the name of the SMTP server. At our office our e-mail is hosted by a third party SMTP server so the SMTP server's name is different than the part of the e-mail address after the '@'. Also make sure the SMTP server doesn't require authentication. If it does, you'll need to use Net::SMTP_auth instead. You can check your mail client's (Outlook's) settings to be sure.

 
i pretty much go it alone with programming so if it ain't loaded it ain't gonna' be.

Modules don't have to be loaded on your system in order for you to use them! You just have to put a couple of extra lines of code in your script to call the library or module that you place on your server.

Example: Create a sub-directory called "modules" in your cgi-bin directory. Place the modules you want to use in this directory. Then, in your script, you would do something like this:

Code:
use lib qw(path to your webserver/domainname.com/cgi-bin/modules);

use the_module_you_want;

You may have to tinker with the path layout a bit. I just got through doing one for a client who's site is hosted by a commercial hosting company and this is how the first line looked:

Code:
use lib qw(/home/httpd/vhosts/website_name.com/cgi-bin/modules);

I've used this method many times when the hosting company doesn't have a particular module loaded and won't install it - for whatever reason.

Good luck!

There's always a better way. The fun is trying to find it!
 
great information!! i'll try calling the modules from a cgi-bin dir.

hey, finally a reason to look forward to monday.

btw, in the previous post you mentioned

You should be putting in the name of the SMTP server. At our office our e-mail is hosted by a third party SMTP server so the SMTP server's name is different than the part of the e-mail address after the '@'.

what will the name look like?? our smtp server is in house on the same system as the part of the e-mail address after the '@'. wouldn't the name then be the part of the e-mail address after the '@'??

i found this code in a different forum
Code:
{ 
#use hash 
        my %mail = ( To      => 'to_email_address',
        From    => 'from_email_address',
        Subject => "Rough Diamonds",
        Message => "Message you want in your email message",
        );
        $mail{smtp} = '111.222.3.44';  #your smtp ip address

        sendmail(%mail) || die "\nProblem! $Mail::Sendmail::error\n";
}

is this doable??

thanks for all the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top