Using perl to email. Do I need PHP?

software development

#1

I am kind of new web programming, having just created my first website on dreamhost.
I created a form which sends information to a database, and I would like to send the same information to an email. The script which is run when ‘submit’ is clicked is a perl script, and I would like to add something to send a simple email with the variable I have already created.

I found documentation on how you can send mail in PHP, but I have never written anything in PHP, and I don’t know if there is a way to run a PHP script from my perl script. Is that possible? Otherwise, does Dreamhost support any type of email from a Perl script? I tried running the unix mail command like:
mail ...; but that does not work. I also tried sending the PHP code to the HTLM output, and it looks, but I guess PHP is not run like that.

Thanks for any help!


#2

There’s a number of CPAN modules related to sending email for Perl, several of which Dreamhost already has pre-installed. You can use one of the SMTP-related ones and have it authenticate and connect to your normal mail server for outgoing. Dreamhost also appears to have postfix on the web servers, and the command-line submission tool for that is at /usr/lib/sendmail. Generally if you run “/usr/lib/sendmail -ti” and then paste in an email (headers and all) it’ll take the To and From out of the headers and send it. So that’s the quick and dirty way is just open a pipe filehandle to sendmail and print your email to it.

open MAIL,"|-","/usr/lib/sendmail","-ti";
print MAIL <<“EOF”;
To: someone@somewhere
From: me@here
Subject: foobar

This is the body of the message.
EOF
close MAIL;

note the @ inside the << structure has a \ in front of it because it’s a literal @ and not the marker for an array variable.


#3

It looks like that works. Thanks so much! That is exactly what I was looking for. Is this sendmail too expected to be supported for some time to come?


#4

I doubt this method is going to break on Dreamhost anytime in the foreseeable future, but you might want to consider a slightly different method just the same. Dreamhost comes with the Mail::Sendmail module installed. I would recommend using the module because it is likely to be more portable, more resistant to any chnages that DH might decide to make, and less prone to bugs than piping output to sendmail by hand.