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

I have a file which have some lines of data in it, i want to send a mail with its contents.

Here is the code i am trying to execute, but it is not working as expected.

. . if (-e -s $errorLog) { my @errorContents = read_file($errorLog); print "@errorContents"; sendMail("abc@xyz.com", "Error in build", @errorContents, "abc +@xyz.com"); }

Replies are listed 'Best First'.
Re: send contents of a file in mail
by hippo (Archbishop) on Jun 28, 2023 at 07:39 UTC
    but it is not working as expected

    Alas, that is an insufficient description of the problem as I am sure on re-reading your post you will agree. You've given no sample data, no sample output, no error messages, no indication of what sendMail() might be or where it has been loaded from. The only possible advice one could give at this point (other than to suggest you provide an SSCCE) is to check everything.


    🦛 "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be specific. SSCCE is best.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: send contents of a file in mail
by choroba (Cardinal) on Jun 28, 2023 at 08:14 UTC
    Double quotes interpolate variables including arrays. "abc@xyz.com" replaces @xyz by join $", @xyz which is probably not what you want. Use single quotes or escape the at-sign.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: send contents of a file in mail
by marto (Cardinal) on Jun 28, 2023 at 08:12 UTC
Re: send contents of a file in mail
by harangzsolt33 (Deacon) on Jun 28, 2023 at 15:13 UTC
    I would start out with something that works. I had a working perl script that sends an email, so I tried to extract only the relevant parts of that program, and I am only showing that here. I did not have a chance to test this program after removing 90% of the code, but I think it will still work.

    #!/usr/bin/perl use strict; use warnings; use MIME::Lite; my $FROM = '"Senders Name" <youremail@email.com>'; my $TO = 'destination_email@email.com'; my $CC = 'second_destination@email.com'; # You may leave this blan +k my $SENDFILE = 'file_to_send.txt'; my $MAX_EMAIL_LENGTH = 1000000; # Don't send email if it's too large my $DATA = ReadFile($SENDFILE); $DATA = pack('u', $DATA); # Uuencode file contents length($DATA) <= $MAX_EMAIL_LENGTH or die "\nThe email shouldn't be la +rger than $MAX_EMAIL_LENGTH.\n"; # Send email my $EMAIL_SUBJECT = "UUEncoded File"; my $HTML_MESSAGE = ''; my $TEXT_MESSAGE = "Here is your file:\n\n" . $DATA; my $STATUS = SendMail($FROM, $TO, $CC, $EMAIL_SUBJECT, $TEXT_MESSAGE, +$HTML_MESSAGE); exit; ###################################################### # Reads an entire file in raw mode and returns the contents as a strin +g. # Usage: STRING = ReadFile(FILENAME) sub ReadFile { my $NAME = shift; (-e $NAME && -f $NAME && -s $NAME) or + return ''; my $DATA; open my $FILE, '<:raw', $NAME or return ''; { l +ocal $/; $DATA = <$FILE>; } close $FILE; return (defined $DATA) ? $DA +TA : ''; } # The SendMail() function sends an email with an attachment. Returns 1 + on success or 0 on failure. # Usage: INTEGER = SendMail(FROM, TO, CC, SUBJECT, TEXT_MESSAGE, HTML_ +MESSAGE) sub SendMail { @_ == 6 or return 0; my ($From, $To, $Cc, $Subject, $TEXT, $HTML) = @_; my $EMAIL = MIME::Lite->new( From => $From, To => $To, Cc => $Cc, Subject => $Subject, Type => 'multipart/mixed'); # Add text portion. $EMAIL->attach( Type => 'text', Data => $TEXT); # Add HTML portion. $EMAIL->attach( Type => 'text/html', Disposition => 'inline', Data => $HTML); $EMAIL->send; return 1; }
      How is this mess better than the examples in the synopsis of the module docs? Terrible error handling btw.
      A reply falls below the community's threshold of quality. You may see it by logging in.