Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

MAIL::SENDMAIL - Inserting $variable Into TO or FROM?

by Milti (Beadle)
on Jan 21, 2014 at 17:20 UTC ( [id://1071496]=perlquestion: print w/replies, xml ) Need Help??

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

I'm using the following code to send messages:

sub send_mail { use HTML::Entities; use Mail::Sendmail 0.79; # doesn't work with v. 0.74! $FORM {'Message'}="GOOD MORNING!! It looks as if things are working!!" +; $Message="$FORM{'Message'}"; $FORM{'Sender'}="My Organization"; $sender="$FORM{'Sender'}"; $html = <<END_HTML; <p><strong>A MESSAGE FROM SOMEBODY</strong> <p>$FORM{'Sender'} has sent you a message.</p> <p>$Message</p> END_HTML %mail = ( To => 'somebody@somewhere.com', From => 'me@mysight.com' subject => 'A Message From Somebody', 'content-type' => 'text/html; charset="iso-8859-1"', ); $mail{body} = <<END_OF_BODY; <html>$html</html> END_OF_BODY sendmail(%mail) || print "Error: $Mail::Sendmail::error\n"; }
This works perfectly when email addresses are entered directly into the TO/FROM entities. However, I want to insert a $variable from a form into those slots. I have tried every combination that I can think of to accomplish that but nothing has worked. Can someone provide some guidance? Thanks for any assistance. I have tried the following at TO and FROM:
TO => $VAR, TO => "$VAR", TO => '$VAR', TO => $FORM{'recipient'}, TO => $FORM{recipient}, TO => "$FORM{'recipient}", TO => "$FORM{recipient}",
All without success. The interesting thing is that both $message and $FORM{'Sender'} are interpolated within the HTML message section of the code. $FORM {'recipient'}= "Somebody@somewhere.com"; and $VAR = "$FORM{'recipient}"; . I previously used an old version of Matthew Wright's FORMMAIL to process form input. It handled the following just fine:
sub send_mail { print "content-type: text/html\n\n"; %mail = ( To => "$FORM{recipient}", From => "$FORM{email}", Message => "$msg", Subject => "A Message From Somebody", ); sendmail(%mail) or die $Mail::Sendmail::error; }
The form variables were defined the same as above. Why won't the new code interpolate the variables as well?

Replies are listed 'Best First'.
Re: MAIL::SENDMAIL - Inserting $variable Into TO or FROM?
by kcott (Archbishop) on Jan 21, 2014 at 20:23 UTC

    G'day Milti,

    There's a number of issues with what you've posted:

    • "nothing has worked" and "All without success." provide us with no information about how things didn't work or in what way they were unsuccessful. The guidelines in "How do I post a question effectively?" will help you in this regard.
    • Your apparent reliance on package variables could be a problem: you don't provide sufficient code to tell. I recommend you use the strict pragma and fix any problems it reports: in most cases, this will simply require declaring variables with my.
    • If you're not already doing it, I also recommend you use the warnings pragma: this may highlight problems that aren't immediately obvious.
    • You don't show any declarations or assignments for $VAR or $FORM{recipient}. While testing, you can simply use print to check these have values and what those values are.
    • Consider all data supplied by outside sources to be untrustworthy ("perlsec - Perl security" has a lot more information on this). You should validate the email addresses supplied by users: Regexp::Common::Email::Address may help with this.
    • A common problem with email addresses is the '@' sign. This can make a string appear to have an embedded array. You can use quotemeta to escape the '@', and any other special characters, in the email addresses.

    -- Ken

Re: MAIL::SENDMAIL - Inserting $variable Into TO or FROM?
by Laurent_R (Canon) on Jan 21, 2014 at 18:35 UTC

    Did you try to use double quotes instead of single quotes to enable variable interpolation?

    Update: OK, now you have provided examples of what you tried, this is good, but you should indicate clearly what you changed compared to your original question, because it now looks like my question is useless since you added reports on tests you did with double quotes, but this was not in the OP. Please show a complete example with double quotes (or no quote at all), including how you are defining the variables that you later use for your sender, recipient, etc. The problem might be somewhere else than in the definition of the mail itself.

Re: MAIL::SENDMAIL - Inserting $variable Into TO or FROM?
by kcott (Archbishop) on Jan 22, 2014 at 12:34 UTC

    It would appear that you updated your OP (without indicating such) following Laurent_R's initial response. I was unaware that you had made this update when I initially responded. You now appear to have updated the OP again: there's still no indication of what exactly you've changed.

    Laurent_R has pointed out how this makes the thread problematic. "How do I change/delete my post?" has full details of how to update a node and why you should do it this way. Please edit your OP such that it accurately reflects the changes you have made.

    Casting my mind back a couple of decades, "Matthew Wright's FORMMAIL" was written for Perl4 and '@' characters were not treated specially in strings in Perl4.

    I've already provided a list of things you could do to help with your problem and also to help us to help you. Despite sending me a PM with "Please the update to my post which answers some of the points you mentioned. Any further advise will be appreciated.", I see little evidence that you've actioned any of those points. Perhaps deal with the advice already given before asking for more.

    -- Ken

      Hi to all who have responded. Ken provided the answer. The '@' character in the email addresses was the problem. In a trial I used a \ to escape the @ and the value was interpolated. Thanks to all who responded to my question! Milti
        Yes, this issue with the @ in the address is exactly what I had in mind when I specified:

        including how you are defining the variables that you later use for your sender, recipient

        I thought that this was a likely cause for your problem.

        Glad anyway that you solved it.

        Can you please show your corrected code? Thanks in advance.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-09-16 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.