| 1 | =head1 NOME | ||
| 2 | |||
| 3 | Puzzle::Sendmail - Modulo di invio mail all'utente | ||
| 4 | |||
| 5 | =head1 SINTASSI | ||
| 6 | |||
| 7 | $smail = new ECP::Sendmail::Sendmail ( email => 'bruni@micso.it'); | ||
| 8 | |||
| 9 | $smail->addMultiPartItem(0,$body_css,'text/css',$css_id); | ||
| 10 | $smail->addMultiPartItem(1,$body_plain,'text/plain'); | ||
| 11 | $smail->addMultiPartItem(1,$body_html,'text/html'); | ||
| 12 | |||
| 13 | $smail->subject('Mail Subject'); | ||
| 14 | $smail->send(); | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | =head1 DESCRIZIONE | ||
| 19 | |||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | |||
| 24 | package Puzzle::Sendmail; | ||
| 25 | |||
| 26 | our $VERSION = '0.02'; | ||
| 27 | |||
| 28 | use Params::Validate qw(:types); | ||
| 29 | use base 'Class::Container'; | ||
| 30 | |||
| 31 | use Puzzle::Sendmail::MultiPartItem; | ||
| 32 | use Mail::Sendmail; | ||
| 33 | |||
| 34 | use HTML::Mason::MethodMaker( | ||
| 35 | read_write => [ | ||
| 36 | [ body => { parse => 'string', type => SCALAR} ], | ||
| 37 | [ subject => { parse => 'string', type => SCALAR} ], | ||
| 38 | [ to => { parse => 'string', type => SCALAR} ], | ||
| 39 | ] | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub begin_mail { | ||
| 43 | my $self = shift; | ||
| 44 | $self->{mpId} = 0; | ||
| 45 | delete $self->{mpi}; | ||
| 46 | delete $self->{mail}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub addMultiPartItem { | ||
| 50 | my $self = shift; | ||
| 51 | my $level = shift; | ||
| 52 | my ($body,$contentType,$contentId, | ||
| 53 | $contentTransferEncoding, $contentDisposition) = @_; | ||
| 54 | $contentType = 'text/plain' unless ($contentType); | ||
| 55 | $contentTransferEncoding = 'quoted-printable' unless ($contentTransferEncoding); | ||
| 56 | # $self->{mpId} contiene un incrementale del numero di MultiPartItem | ||
| 57 | # aggiunti a questa istanza di sendmail | ||
| 58 | my $mp = new Puzzle::Sendmail::MultiPartItem( | ||
| 59 | id => $self->{mpId}++, | ||
| 60 | contentType => $contentType, | ||
| 61 | contentId => $contentId, | ||
| 62 | body => $body, | ||
| 63 | contentTransferEncoding => $contentTransferEncoding, | ||
| 64 | contentDisposition => $contentDisposition | ||
| 65 | ); | ||
| 66 | push @{$self->{mpi}->[$level]},$mp; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub end_mail { | ||
| 71 | my $self = shift; | ||
| 72 | ${$self->{mail}}{subject} = $self->subject; | ||
| 73 | ${$self->{mail}}{to} = $self->to; | ||
| 74 | ${$self->{mail}}{smtp} = $self->container->cfg->mail->{server}; | ||
| 75 | ${$self->{mail}}{from} = $self->container->cfg->mail->{from}; | ||
| 76 | $self->builtMail; | ||
| 77 | return sendmail(%{$self->{mail}}) or die $self->error; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub error { | ||
| 81 | my $self = shift; | ||
| 82 | return $Mail::Sendmail::error; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub log { | ||
| 86 | my $self = shift; | ||
| 87 | return $Mail::Sendmail::log; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub property { | ||
| 91 | my $self = shift; | ||
| 92 | my $key = shift; | ||
| 93 | if (@_) { | ||
| 94 | my ($value) = @_; | ||
| 95 | ${$self->{mail}}{$key} = $value; | ||
| 96 | } | ||
| 97 | return ${$self->{mail}}{$key}; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub builtMail { | ||
| 101 | # genera il mail | ||
| 102 | my $self = shift; | ||
| 103 | my $body; | ||
| 104 | my @boundary; | ||
| 105 | # Consideriamo, per ora, due livelli | ||
| 106 | $boundary[0] = '====0' . time() . '0===='; | ||
| 107 | ${$self->{mail}}{'Content-Type'} = "multipart/related; " . | ||
| 108 | "type=\"multipart/alternative\"; boundary=\"$boundary[0]\""; | ||
| 109 | $body .= "--" . $boundary[0] . "\n"; | ||
| 110 | $boundary[1] = '====1' . time() . '1===='; | ||
| 111 | $body .= "Content-type: " . | ||
| 112 | "multipart/alternative; boundary=\"$boundary[1]\"\n\n"; | ||
| 113 | foreach my $lev1 (@{$self->{mpi}->[1]}) { | ||
| 114 | $body .= "--" . $boundary[1] . "\n"; | ||
| 115 | $body .= $lev1->toString; | ||
| 116 | } | ||
| 117 | $body .= "--" . $boundary[1] . "--\n\n"; | ||
| 118 | foreach my $lev0 (@{$self->{mpi}->[0]}) { | ||
| 119 | $body .= "--" . $boundary[0] . "\n"; | ||
| 120 | $body .= $lev0->toString; | ||
| 121 | } | ||
| 122 | $body .= "--" . $boundary[0] . "--\n\n"; | ||
| 123 | ${$self->{mail}}{message} = $body; | ||
| 124 | } | ||
| 125 | |||
| 126 | # ---------------------------- BEGIN POD ---------------------------------- | ||
| 127 | |||
| 128 | |||
| 129 | =head1 AUTORE | ||
| 130 | |||
| 131 | Emiliano Bruni, bruni@micso.it | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | # ----------------------------- END POD ----------------------------------- | ||
| 136 | |||
| 137 | 1; |