Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Need to send an email through my SMTP server using Perl

by Logic_Bomb421 (Novice)
on Aug 29, 2014 at 18:37 UTC ( [id://1099030]=perlquestion: print w/replies, xml ) Need Help??

Logic_Bomb421 has asked for the wisdom of the Perl Monks concerning the following question:

So like the title says, I need to send an email via my SMTP server. It's just a simple test email designed to verify the SMTP server is active and doing its job. Here's what I have so far (with email addresses removed):

#!/usr/bin/perl use lib "/path/to/Net::SMTP/module"; use Net::SMTP $host = "SMTP SERVER"; $port = 25; $smtp = Net::SMTP->new($host, Hello => port=>$port) $smtp->mail("FROM EMAIL ADDRESS"); $smtp->recipient("TO EMAIL ADDRESS"); $smtp->data; $smtp->datasend("From: host\@domain.com"); $smtp->datasend("To: account\@domain.com\n"); $smtp->datasend("Subject: Test"); $smtp->datasend("\n"); $smtp->datasend ("This is a test"); $smtp->dataend; $smtp->quit;

When I run this, I get the error:

"SMTP SERVER" is not exported by the Net::SMTP module. Can't continue +after import errors at line 6.

What does it mean "is not exported"?

Replies are listed 'Best First'.
Re: Need to send an email through my SMTP server using Perl
by toolic (Bishop) on Aug 29, 2014 at 18:46 UTC
    Add a semicolon. Change:
    use Net::SMTP

    to:

    use Net::SMTP;

    You need one after your "new" line, too.

    I figured this out by deleting most of your lines of code until the error disappeared.

      It's always a damn semicolon! Ah well. I went ahead and fixed those and now it's saying it can't call method put() on an undefined value.

        Even if you clean up personal information, you should still post code that will run. Also, be sure to post the entire error message. Did you delete the content you're assigning to 'Hello'? That would yield something like the error you mention in your reply. It should do a decent job of picking a helo line for you.
        use strict; use warnings; use Net::SMTP; $host = "mail.host.com"; $port = 25; $smtp = Net::SMTP->new($host, port=>$port); $smtp->mail("test\@domain.com"); $smtp->recipient("test\@domain2.com"); $smtp->data; $smtp->datasend("From: test\@domain.com"); $smtp->datasend("To: test\@domain2.com"); $smtp->datasend("Subject: Test"); $smtp->datasend("\n"); $smtp->datasend ("This is a test"); $smtp->dataend; $smtp->quit;
        Really you're going to find it much easier in the long wrong (espcially to manage) to just utilize Email::Sender::Simple with Email::Simple::Creator: here's an easy to understand example:
        my $email = Email::Simple->create( header => [ To => $p->{email_to}, Cc => $p->{email_cc}, From => $CONF->{smtp}->{from}, Subject => $sub ], body => $str ); if($DRYRUN){ print "---- EMAIL ----\n${$email->{body}}\n---- END FO + EMAIL ----\n" if $DEBUG; } elsif($CONF->{smtp}->{enabled}) { # Do some more emailing and stuff below... my $transport; $transport = Email::Sender::Transport::SMTP->new({ host => $CONF->{smtp}->{hos +t}, port => $CONF->{smtp}->{por +t}, ssl => $CONF->{smtp}->{ssl +}, sasl_username => $CONF->{smtp}->{sas +l_username}, sasl_password => $CONF->{smtp}->{sas +l_password}, allow_partial_success => $CONF->{smtp}->{all +ow_partial_success}, helo => $CONF->{smtp}->{hel +o}, localaddress => $CONF->{smtp}->{loc +aladdress}, localport => $CONF->{smpt}->{loc +alport}, timeout => $CONF->{smpt}->{tim +eout} }); sendmail($email, { transport => $transport }); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1099030]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found