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

This is the code I'm using for windows.Please check it and give suggestions for the code in the ways of giving To & From address

use strict; use warnings; use Net::SMTP; $smtp = Net::SMTP->new('$my_host'); $mail_to='abc@gmail.com'; $smtp->mail($ENV{USER}); $smtp->to($mail_to); $smtp->data(); $smtp->datasend("To: $mail_to\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit; print "Email sent successfully\n";

Replies are listed 'Best First'.
Re: Email sending in Activeperl 5 on Windows
by NetWallah (Canon) on Jun 24, 2019 at 14:39 UTC
    Use double quotes on this line,to allow interpolation -- or better NO QUOTES:
    $smtp = Net::SMTP->new($my_host);
    I don't now SMTP well enough to verify the rest.

                    Time is an illusion perpetrated by the manufacturers of space.

Re: Email sending in Activeperl 5 on Windows
by bliako (Abbot) on Jun 24, 2019 at 14:40 UTC

    This wont fly: $smtp = Net::SMTP->new('$my_host'); the single quotes stop Perl from interpolating the $my_host so you are trying to connect to a host called $my_host, literally.

    This sets the sender: $smtp->mail($ENV{USER}); and this sets the recipient $smtp->to($mail_to);. But beware that most public SMTP servers nowadays require a valid and full email sender address and so $env{USER} wont cut it as it is just your login name unknown to say yahoo mail server. Unless of course you are using your local mail server. But even in this case, sometimes such scripts as yours are run by cron and the username in that case may be something else than what you think you are sending out.

    Also, I would check the return of each command for success before proceeding to the next, for example:

    my $smtp = Net::SMTP->new('$my_host'); die "error connecting to $my_host" unless defined $smtp; $smtp->mail($ENV{USER}) or die "error issuing MAIL command: ".$smtp->m +essage(); ...

    When you do all these then you have every right to declare Email sent successfully

    5' Edit: for debugging purposes, one can talk to the SMTP server manually via telnet (if it does not require to talk TLS or SSL). Net::SMTP replaces the following:

    telnet mysmtphost.com 25 HELO myip MAIL FROM: myemail@abc.com RCPT TO: recipient@xyz.com DATA helo there blah blah .

    hacky days