my second plugin! :)
| From: | Rasmus Schultz | Date: | Wed, 05 Jun 2002 01:13:58 +0000 |
| Subject: | my second plugin! :) | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-4588@lists.php.net to get a copy of this message | ||
Here's my second plugin, tested and (apparently) working fine :)
- as with my first plugin, however, I'd like your feedback and
a chance to clear up any errors before it's posted on the
contribs page.
It's a block function plugin, that you can use to generate and
send email directly from a template - useful for sending out
emails on demand (for example a lost password) ...
Here's an example:
{sendmail subject="This is a test mail" to_name="Rasmus Schultz"
to_email="mindplay@mindplay.dk" from_name="Rasmus Twin"
from_email="eviltwin@mindplay.dk"}
Dear Sir,
Just an email to let you know, your new sendmail plugin works.
Have a nice day.
{/sendmail}
This example is quite simple, but a lot of other optional
parameters are available, for example you can send HTML mail,
change priority, or set a reply-to address.
In combination with the MySQL plugin and {if} sections, I think
this little plugin could be quite useful? :)
Now here's the source:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: block.sendmail.php
* Type: block
* Name: sendmail
* Version: 0.1
* Date: June 5th, 2002
* Purpose: Send an email using the mail() function,
* everything within the block will be sent as email.
* Input: from_name = Sender's name
* from_email = Sender's email address
* to_name = Recipient's name
* to_email = Recipient's email address
* replyto_name = Reply-to name (optional, defaults
to same asfrom_name)
* replyto_email = Reply-to email address (optional,
defaults to same as from_email)
* priority = Optional, defaults to 3 (Normal)
* subject = Subject of email message
* format = Either "html" or "plain",
optional, defaults to "plain"
* Author: Rasmus Schultz <mindplay(at)mindplay.dk>
* -------------------------------------------------------------
*/
function smarty_block_sendmail($params, $content, &$smarty)
{
if ($content) {
extract($params);
if (empty($format)) {
$format = "plain";
}
if (empty($to_name)) {
$smarty->trigger_error("sendmail: missing 'to_name' parameter");
return;
}
if (empty($to_email)) {
$smarty->trigger_error("sendmail: missing 'to_email' parameter");
return;
}
if (empty($from_name)) {
$smarty->trigger_error("sendmail: missing 'from_name' parameter");
return;
}
if (empty($from_email)) {
$smarty->trigger_error("sendmail: missing 'from_email' parameter");
return;
}
if (empty($subject)) {
$smarty->trigger_error("sendmail: missing 'subject' parameter");
return;
}
if (empty($replyto_name)) {
$replyto_name = $from_name;
}
if (empty($replyto_email)) {
$replyto_email = $from_email;
}
if (empty($priority)) {
$priority = "3";
}
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/".$format."; charset=iso-8859-1\r\n";
$headers .= "From: ".$from_name."<".$from_email.">\r\n";
$headers .= "To: ".$to_name."<".$to_email.">\r\n";
$headers .= "Reply-To:
".$replyto_name."<".$replyto_email.">\r\n";
$headers .= "X-Priority: ".$priority."\r\n";
$headers .= "X-Mailer: smarty.sendmail.php";
mail($to_email, $subject, $content, $headers);
}
}