| 1 | ############################################################################### | ||
| 2 | # XML::Template::Element::Email | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Element::Email; | ||
| 11 | use base qw(XML::Template::Element); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use Mail::Sender; | ||
| 15 | use HTML::Strip; | ||
| 16 | use IO::String; | ||
| 17 | |||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | XML::Template::Element::Email - XML::Template plugin module for the | ||
| 24 | email namespace. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | This XML::Template plugin module implements the email namespace tagset. | ||
| 29 | The block namespace includes tags that handle sending email. | ||
| 30 | |||
| 31 | =head1 CONSTRUCTOR | ||
| 32 | |||
| 33 | XML::Template::Element::Block inherits its constructor method, C<new>, | ||
| 34 | from L<XML::Template::Element>. | ||
| 35 | |||
| 36 | =head1 EMAIL TAGSET METHODS | ||
| 37 | |||
| 38 | =head2 send | ||
| 39 | |||
| 40 | This method implements the send tag which sends email. The text of the | ||
| 41 | message is contained in the content. The following attributes are used: | ||
| 42 | |||
| 43 | =over 4 | ||
| 44 | |||
| 45 | =item from_addr | ||
| 46 | |||
| 47 | The email address of who is sending the email. | ||
| 48 | |||
| 49 | =item to_addr | ||
| 50 | |||
| 51 | The email address of who the email is being sent to. | ||
| 52 | |||
| 53 | =item subject | ||
| 54 | |||
| 55 | The subject of the email. | ||
| 56 | |||
| 57 | =item type | ||
| 58 | |||
| 59 | If this is set to C<html>, the email will be sent with the content type | ||
| 60 | set to C<text/html>. If set to C<plain>, the email will be stripped of | ||
| 61 | HTML and the content type will be set to C<text/plain>. | ||
| 62 | |||
| 63 | =item smtp | ||
| 64 | |||
| 65 | The IP address of domain name of the mail server you wish to use. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub send { | ||
| 72 | my $self = shift; | ||
| 73 | my ($code, $attribs) = @_; | ||
| 74 | |||
| 75 | my $from_addr = $self->get_attrib ($attribs, 'from_addr') || 'undef'; | ||
| 76 | my $to_addr = $self->get_attrib ($attribs, 'to_addr') || 'undef'; | ||
| 77 | my $subject = $self->get_attrib ($attribs, 'subject') || 'undef'; | ||
| 78 | my $type = $self->get_attrib ($attribs, 'type') || 'plain'; | ||
| 79 | my $smtp = $self->get_attrib ($attribs, 'smtp') || 'undef'; | ||
| 80 | |||
| 81 | my $outcode = qq{ | ||
| 82 | do { | ||
| 83 | use Mail::Sender; | ||
| 84 | use HTML::Strip; | ||
| 85 | |||
| 86 | my \$ctype; | ||
| 87 | if ($type eq 'plain') { | ||
| 88 | \$ctype = 'text/plain'; | ||
| 89 | } else { | ||
| 90 | \$ctype = 'text/html'; | ||
| 91 | } | ||
| 92 | # XXX | ||
| 93 | my \$sender = Mail::Sender->new ({smtp => $smtp}); | ||
| 94 | if (\! \$sender->Open ({from => $from_addr, | ||
| 95 | to => $to_addr, | ||
| 96 | subject => $subject, | ||
| 97 | ctype => \$ctype, | ||
| 98 | encoding=> '7bit'})) { | ||
| 99 | print "<h3>Sending Email Failed.</h3>"; | ||
| 100 | } else { | ||
| 101 | my \$html; | ||
| 102 | my \$io = IO::String->new (\$html); | ||
| 103 | my \$ofh = select \$io; | ||
| 104 | $code | ||
| 105 | select \$ofh; | ||
| 106 | |||
| 107 | my \$text; | ||
| 108 | if ($type eq 'plain') { | ||
| 109 | my \$hs = HTML::Strip->new (); | ||
| 110 | \$text = \$hs->parse (\$html); | ||
| 111 | } else { | ||
| 112 | \$text = \$html; | ||
| 113 | } | ||
| 114 | |||
| 115 | \$sender->print (\$text); | ||
| 116 | \$sender->Close (); | ||
| 117 | print "Email Successfully Sent."; | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | }; | ||
| 121 | |||
| 122 | return ($outcode); | ||
| 123 | } | ||
| 124 | |||
| 125 | =pod | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | Jonathan Waxman | ||
| 130 | <jowaxman@bbl.med.upenn.edu> | ||
| 131 | |||
| 132 | =head1 COPYRIGHT | ||
| 133 | |||
| 134 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 135 | All rights reserved. | ||
| 136 | |||
| 137 | This program is free software; you can redistribute it and/or | ||
| 138 | modify it under the same terms as Perl itself. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | |||
| 143 | 1; |