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

Hi Everyone, I am using MIME::Lite to establish email connection, I am doing fine with the module. The problem is to pass parameter through this subroutine, my intention is to show the value of this parameter in email subject and body. But my script does not show the value properly. Can anybody help?
&SendMail($parameter); sub SendMail() { my $service = @_; my $msgTo = 'xxx'; my $msgFrom = 'xxx'; my $smtphost = 'xxx.com'; my $smtpport = 25; my $msgSubject = '$service '; my $msg = MIME::Lite->new ( From => $msgFrom, To => $msgTo, Subject => $msgSubject, Type =>'multipart/alternative', ) or die "Error creating multipart container: $!\n"; # Create the text part my $text_part = MIME::Lite::->new( 'Type' => 'text/plain', 'Data' => 'Hi $service', ); # Create the HTML part my $html_part = MIME::Lite::->new( 'Type' => 'multipart/related', ); $html_part->attach( 'Type' => 'text/html', 'Data' => 'Hi $service', ); $msg->attach($text_part); $msg->attach($html_part); my $email = $msg->as_string(); my $smtp = Net::SMTP_auth->new($smtphost, Port=>$smtpport) or die "Can +'t connect"; #$smtp->auth('PLAIN', $smtpuser, $smtppass) or die "Can't authenticate +:".$smtp->message(); $smtp->mail($msgFrom) or die "Error:".$smtp->message(); $smtp->recipient($msgTo) or die "Error:".$smtp->message(); $smtp->data() or die "Error:".$smtp->message(); $smtp->datasend($email) or die "Error:".$smtp->message(); $smtp->dataend() or die "Error:".$smtp->message(); $smtp->quit or die "Error:".$smtp->message(); return; }

Replies are listed 'Best First'.
Re: Pass parameter in email subject and body through MIME::Lite;
by Corion (Patriarch) on Sep 30, 2019 at 16:43 UTC

    Have you checked that your problem is related to MIME::Lite at all?

    The following program shows that you never read the parameters properly:

    #!perl use strict; use warnings; my $parameter = 'Hello World'; &SendMail($parameter); sub SendMail() { my $service = @_; print "Service is: [$service]\n"; my $msgSubject = '$service '; print "msgSubject is: [$msgSubject]\n"; }

    Single quotes do not interpolate variables.

    Parameters need to assigned in subroutines as:

    my ($service) = @_;
Re: Pass parameter in email subject and body through MIME::Lite;
by marto (Cardinal) on Sep 30, 2019 at 16:51 UTC

    Single vs Double quotes.

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; my $subject = 'Citizens, not subjects!'; sub wrong{ my ( $emailsubject ) = @_; say 'Email subject: $emailsubject'; } sub right{ my ( $emailsubject ) = @_; say "Email Subject: $emailsubject"; } wrong( $subject ); right( $subject );

    Strings in Perl: quoted, interpolated and escaped.