Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:07:45+00:00 2026-05-25T15:07:45+00:00

I need to write a perl script for sending emails. The script should read

  • 0

I need to write a perl script for sending emails.
The script should read .txt document containing the e-mail addresses as its first argument (there could be more than one address, and they are all separated with “;”) and an .html document that should be the body of the e-mail as its second.

#!/usr/bin/perl -w
use Net::SMTP::SSL;

sub send_mail {

    my $to = $ARGV[0];
    open(MYFILE, $to) || die("Could not open file!");
    @recepients=<MYFILE>;
    close(MYFILE);

    my $body = $ARGV[1];
    open (TXTFILE, $body);
    @lines = <TXTFILE>;
    close(TXTFILE);
    $body = join("",@lines);

    my $from = 'theAddress@gmail.com';
    my $password = 'thePassword';

    my $smtp;

    if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',
                            Port => 465,
                            Debug => 1)) {
        die "Could not connect to server\n";
    }

    $smtp->auth($from, $password) || die "Authentication failed!\n";

    $smtp->mail($from . "\n");

    my @recepients = split(/;/, $to);
    foreach my $recp (@recepients) {
        $smtp->to($recp . "\n");
    }
    $smtp->data();
    $smtp->datasend("From: " . $from . "\n");
    $smtp->datasend("To: " . $to . "\n");
    $smtp->datasend("Subject: " . $subject . "\n");
    $smtp->datasend("\n");
    $smtp->datasend($body . "\n");
    $smtp->dataend();
    $smtp->quit;
}

&send_mail()

So I tried to work something out, but I have a problem with extracting the information both from the .txt and .html documents. So the error should be somewhere at the splitting of the recipients.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:07:46+00:00Added an answer on May 25, 2026 at 3:07 pm

    There were several issues in your script. I would suggest you to use Perl::Critic as it analyzes your code and usually gives helpful hints.

    The following works:

    #!/usr/bin/env perl
    

    Always use strict and warnings

    use strict;
    use warnings;
    
    use Net::SMTP::SSL;
    

    English will give a textual representation of error messages

    use English qw(-no_match_vars);
    

    Carp issues warnings and error from the perspective of the caller

    use Carp;
    
    our $VERSION = '1.0.0';
    
    sub send_mail {
    
        my ( $to, $body ) = @_;
    

    it is better to have filehandles as variables too

        my $to_handle;
        my $text_handle;
    

    Always declare variables

        my @recipients;
        my @lines;
    

    Always check the return value of system calls (open, close, …)

        # print the error message in case of errors
        open $to_handle, '<', $to
          or croak "Error opening $to: $OS_ERROR";
        @recipients = <$to_handle>;
        close $to_handle
          or croak "Error closing $to: $OS_ERROR";
    
        open $text_handle, '<', $body
          or croak "Error opening $body: $OS_ERROR";
        @lines = <$text_handle>;
        close $text_handle
          or croak "Error closing $body: $OS_ERROR";
    
        $body = join '', @lines;
    
        my $from     = '.....@gmail.com';
    

    I would avoid putting a password in the script source

        my $password = '*****';
    
        my $smtp;
    

    Don’t put a new line at the end of die/warn/… as it will remove the line number where the error occurred

        $smtp = Net::SMTP::SSL->new(
            'smtp.gmail.com',
            Port  => 465,
            Debug => 1
        ) or croak 'Could not connect to server';
    
        $smtp->auth( $from, $password )
          or croak 'Authentication failed!';
    
        $smtp->mail( $from . "\n" );
    
        # removed trailing \n
        chomp $recipients[0];
    

    The ; separated list is in the first line (the first element of your array): you have to split the first line.

        foreach my $recp ( split /;/mxs, $recipients[0] ) {
            $smtp->to( $recp . "\n" );
        }
    
        $smtp->data();
        $smtp->datasend( "From: $from\n" );
        $smtp->datasend( "To: $to\n" );
    

    $subject was not defined, you would have detected it with strict and warnings

        $smtp->datasend("Subject: Test\n");
        $smtp->datasend("\n");
        $smtp->datasend("$body\n" );
        $smtp->dataend();
        $smtp->quit;
    

    It is a good practice to end subroutines with a return as perl uses the result of the last evaluation as a result if return is not specified.

        return;
    
    }
    

    Evaluate ARGV in the main body. You will loose clarity if you scatter the processing in one or several subroutines

    if ( !$ARGV[0] || !$ARGV[1] ) {
        print STDERR "usage: send to content\n";
        exit 1;
    }
    

    There was a missing semicolon. You don’t need to use & to call subroutines

    send_mail( $ARGV[0], $ARGV1 );

    1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a Perl script to read in a file, and delete
I need to write a Perl script that pipes input into a Java program.
Im trying to write a perl script to parse a directory full of emails
I need to write a java script. This is supposed to validate if the
I have a Perl script on a Linux (Ubuntu 8.10) machine and I need
I have these two lines in an old Perl script. When I write the
I need to write a script that writes (appends) data to an internal wiki
I am working on a Perl script to read CSV file and do some
I'm trying to write a perl script that determines which users are currently logged
To compile a C++ project, I want to write a perl script to compile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.