Thanks for the suggestions, guys. It's some serious Frankencode, but I've got the script configured to use a separate text file as the body of the email it sends. I do have one last issue, however:
How do I configure it so that the email also displays three of the variables defined by the script? Basically, I've got "var1", "var2", and "var3", which are all defined by the user in the course of the script running. Ultimately, I want those user-defined variables to display in the notification email sent by the script.
I have a feeling that this is a super simple thing, but again...lost. As you can probably tell, my grasp on the terminology is so tenuous that it's difficult for me to even frame the question cogently. Bah.
OLD QUESTION:
So, I just got a "throwaway" assignment at work. The problem is, it's not something I'm familiar with. At all. For the last several months, I've been writing PHP to interact with various Postgres pages, and today I was told to do something with Perl. I've got virtually no experience with Perl, and have been learning as I go with Postgres and PHP anyhow, so I'm pretty much lost right now.
The assignment is to write a Perl script that:
- Creates a new user on a database.
- Sends that user an email, notifying them of the hostname, their username, and their password.
- Uses as the body of that email a text file template that can be edited independently.
It was possibly just a flip comment, but woman who gave me this assignment said that it could be done with "ten lines of actual code". Perhaps it can. I'm sure that with a psql command, it would be pretty quick. I know it's simple, but I am clueless where to start.
A straight-up answer would be fine, but isn't necessary. Just being pointed to a specific resource that can help walk me through this would be super.
Thanks.
Posts
- take a username on the command-line
- connect to the DB
- run the SQL command to create a new user, with the password something random
- read in an email to send (I'd use HTML::Template for this. It doesn't have to be HTML it templates... I used it for emails all the time)
- send an email (MIME::Lite is probably the winner here)
10 lines is an exaggeration (but "of actual code" might be right I guess. Depends what you exclude...), but you could do it in less than 100.
For the template, as Lewisham suggested, HTML::Template is great. The other template system I commonly use is Template-Toolkit (just Template.pm, so from cpan you use 'install Template' even though due to the name, your first thought is "install Template::Toolkit").
For sending an e-mail, all I've ever used is Net::SMTP. It's pretty simple to use. MIME::Lite may be better, though, I've never used it and it's been years since I needed to write a perl app that sent an e-mail.
edit: now it makes sense because I don't keep referencing mysql
How do I configure it so that the email also displays three of the variables defined by the script? Basically, I've got "var1", "var2", and "var3", which are all defined by the user in the course of the script running. Ultimately, I want those user-defined variables to display in the notification email sent by the script.
I have a feeling that this is a super simple thing, but again...lost. As you can probably tell, my grasp on the terminology is so tenuous that it's difficult for me to even frame the question cogently. Bah.
Twitter | Facebook | Tumblr | Last.fm | Pandora | LibraryThing | formspring | Blue Moon over Seattle (MCFC)
"Here is the body of my email, <TMPL_VAR var_1> is the best variable, <TMPL_VAR var_2> is the second best, etc etc etc"
and then you'd create a new HTML::Template object, reading in the body, like:
and set the params like
and finally use $template->output(); to output your text
Personally though, i would just use a regular expression since this doesn't seem like a huge template.
Use something like <var_1> to denote where your variable will be in the body, and then do
The first <var_1> matches that text, and replaces it with the $var_1 which you've defined elsewhere in the code