Revision 740

Date:
2018/11/12 22:03:41
Author:
ahitrov
Revision Log:
in-core e-mail sender

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/core/GNUmakefile

     
    95 95 project_reload reload \
    96 96 project_fullreload full \
    97 97 project_rewind rewind nano \
    98 project_fullrewind giga \
    98 99 project_refresh refresh \
    99 100 project_deinstall pdi \
    100 101 project_user user \
     
    963 964 project_conf project_start
    964 965 @echo $@ done
    965 966
    967 # full reinstall & restart core & project
    968 giga: project_fullrewind ;
    969 project_fullrewind:: project_stop mason_clean \
    970 core_install project_install plugins_install \
    971 project_conf project_start
    972 @echo $@ done
    973
    966 974 # full reinstall & restart project
    967 975 reload: project_reload ;
    968 976 project_reload:: project_stop mason_clean project_update project_install \
     
    1531 1539 DEFAULT_ESCAPE_FLAGS \
    1532 1540 DEFAULT_HANDLER \
    1533 1541 DEVELOPMENT \
    1542 EMAIL_ENABLE \
    1543 EMAIL_MAILER \
    1544 EMAIL_FROM \
    1545 EMAIL_LOGIN \
    1546 EMAIL_PASSWORD \
    1547 EMAIL_SMTP_SERVER \
    1548 EMAIL_SMTP_HELLO \
    1549 EMAIL_SMTP_TIMEOUT \
    1550 EMAIL_SMTP_SSL \
    1551 EMAIL_SMTP_PORT \
    1534 1552 ERROR_MODE \
    1535 1553 FILES \
    1536 1554 FILE_WEB_STORAGE \
  • utf8/core/lib/Contenido/Mail.pm

     
    1 package Contenido::Mail;
    2
    3 use Net::SMTP;
    4 use MIME::Lite;
    5 use MIME::Base64;
    6 use Data::Dumper;
    7 use Contenido::Globals;
    8
    9 sub new {
    10 my ($proto) = @_;
    11 my $class = ref($proto) || $proto;
    12 my $self = {};
    13
    14 $self->{enable} = $state->{email_enable};
    15 if ( $self->{enable} ) {
    16 $self->{mailer} = $state->{email_mailer};
    17 $self->{from} = $state->{email_from};
    18 $self->{login} = $state->{email_auth_login};
    19 $self->{password} = $state->{email_auth_password};
    20 if ( $self->{mailer} eq 'smtp' ) {
    21 $self->{server} = $state->{email_smtp_server};
    22 $self->{hello} = $state->{email_smtp_hello};
    23 $self->{timeout} = $state->{email_smtp_timeout};
    24 $self->{ssl} = $state->{email_smtp_ssl};
    25 $self->{port} = $state->{email_smtp_port};
    26 }
    27 }
    28
    29 bless $self, $class;
    30 return $self;
    31 }
    32
    33 sub send {
    34 return unless @_;
    35 my $self;
    36 if ( ref $_[0] eq 'Contenido::Mail' ) {
    37 $self = shift;
    38 } else {
    39 $self = Contenido::Mail->new;
    40 }
    41
    42 my $opts = shift // {};
    43
    44 my $email = delete $opts->{email} // return undef;
    45 return unless ref $email && exists $email->{to} && $email->{subject} && $email->{body};
    46
    47 my $etype = delete $opts->{etype} // 'mixed';
    48 my $debug = $state->development || $DEBUG || $opts->{debug} ? 1 : 0;
    49
    50 my $subject = $email->{subject};
    51 $subject = MIME::Base64::encode($subject);
    52 $subject =~ s/\s//sgi;
    53 $subject = '=?utf-8?B?'.$subject.'?=';
    54
    55 my $emailfrom;
    56 if ( $email->{from} ) {
    57 my ($from, $efrom) = $email->{from} =~ /^(.*?)<(.*?)>/ ? ($1, $2) : $email->{from} =~ /<(.*?)>/ ? ('',$1) : ('',$email->{from});
    58 if ( $from ) {
    59 $from = MIME::Base64::encode($from);
    60 $from =~ s/\s+$//si;
    61 $from = '=?utf-8?B?'.$from.'?=';
    62 $emailfrom = $from.' <'.$efrom.'>';
    63 } else {
    64 $emailfrom = $efrom;
    65 }
    66 } elsif ( $state->{email_from} ) {
    67 $emailfrom = $self->{from}
    68 }
    69
    70 my ($emailto, @to);
    71 if ( ref $email->{to} eq 'ARRAY' ) {
    72 foreach my $tostr ( @{$email->{to}} ) {
    73 my ($to, $eto) = $tostr =~ /^(.*?)<(.*?)>/ ? ($1, $2) : $tostr =~ /<(.*?)>/ ? ('',$1) : ('',$tostr);
    74 if ( $to ) {
    75 $to = MIME::Base64::encode($to);
    76 $to =~ s/\s+$//si;
    77 $to = '=?utf-8?B?'.$to.'?=';
    78 push @to, $to.' <'.$eto.'>';
    79 } else {
    80 push @to, $eto;
    81 }
    82 }
    83 $emailto = shift @to;
    84 } else {
    85 my ($to, $eto) = $email->{to} =~ /^(.*?)<(.*?)>/ ? ($1, $2) : $email->{to} =~ /<(.*?)>/ ? ('',$1) : ('',$email->{to});
    86 if ( $to ) {
    87 $to = MIME::Base64::encode($to);
    88 $to =~ s/\s+$//si;
    89 $to = '=?utf-8?B?'.$to.'?=';
    90 $emailto = $to.' <'.$eto.'>';
    91 } else {
    92 $emailto = $eto;
    93 }
    94 }
    95
    96 my $ccmail;
    97 if ( exists $email->{cc} && ref $email->{cc} eq 'ARRAY' ) {
    98 foreach my $cc ( @{ $email->{cc}} ) {
    99 my ($cce, $ecce) = $cc =~ /^(.*?)<(.*?)>/ ? ($1, $2) : $cc =~ /<(.*?)>/ ? ('',$1) : ('',$cc);
    100 $cc = $ecce;
    101 }
    102 $ccmail = join ', ', (@to, @{$email->{cc}});
    103 } elsif ( exists $email->{cc} && $email->{cc} ) {
    104 my ($cce, $ecce) = $email->{cc} =~ /^(.*?)<(.*?)>/ ? ($1, $2) : $email->{cc} =~ /<(.*?)>/ ? ('',$1) : ('',$email->{cc});
    105 $ccmail = join ', ', (@to, $ecce);
    106 } elsif ( @to ) {
    107 $ccmail = join ', ', @to;
    108 }
    109
    110 my $body = $email->{body};
    111 warn Dumper($email) if $debug;
    112 my $dt = Contenido::DateTime->new;
    113 $dt->set_locale('en_EN');
    114 my $pdate = $dt->strftime("%a, %d %b %Y %H:%M:%S %z");
    115 my $msg = MIME::Lite->new(
    116 To => $emailto,
    117 From => $emailfrom,
    118 $ccmail ? ( Cc => $ccmail ) : (),
    119 Subject => $subject,
    120 # Encoding=> 'binary',
    121 Date => $pdate,
    122 Type => ($etype eq 'mixed' ? 'multipart/mixed' : $etype eq 'related' ? 'multipart/related;type="multipart/alternative";charset="utf-8"' : $etype),
    123 );
    124 $msg->attach(
    125 'Type' => 'text/html;charset="utf-8"',
    126 'Data' => $body,
    127 'Disposition' => '',
    128 );
    129
    130 my $email_body = $msg->as_string;
    131 if ( $self->mailer eq 'smtp' ) {
    132 my $mailer = Net::SMTP->new( $self->{server},
    133 $self->{hello} ? (Hello => $self->{hello}) : (),
    134 Port => $self->{port},
    135 Timeout => $self->{timeout},
    136 SSL => $self->{ssl},
    137 Debug => $debug,
    138 );
    139 warn Dumper $mailer if $debug;
    140 if ( ref $mailer ) {
    141 if ( $self->{login} && $self->{password} ) {
    142 $mailer->auth( $self->{login}, $self->{password} );
    143 }
    144 $mailer->mail( $emailfrom );
    145 $mailer->to( $emailto );
    146 $mailer->data;
    147 $mailer->datasend( $email_body );
    148 $mailer->dataend;
    149 $mailer->quit;
    150 } else {
    151 warn "MAIL ERROR! Can't connect to Yandex SMTP\n";
    152 }
    153 }
    154 }
    155
    156
    157 1;
  • utf8/core/lib/Contenido/State.pm.proto

     
    144 144 $self->{preamble_handler} = '@PREAMBLE_HANDLER@';
    145 145 $self->{preamble_handler_path} = '@PREAMBLE_HANDLER_PATH@';
    146 146
    147 $self->{email_enable} = lc('@EMAIL_ENABLE@') eq 'yes' ? 1 : 0;
    148 if ( $self->{email_enable} ) {
    149 $self->{email_mailer} = lc ('@EMAIL_MAILER@') eq 'smtp' ? 'smtp' : 'sendmail';
    150 $self->{email_from} = '@EMAIL_FROM@';
    151 $self->{email_auth_login} = '@EMAIL_LOGIN@';
    152 $self->{email_auth_password} = '@EMAIL_PASSWORD@';
    153 if ( $self->{email_mailer} eq 'smtp' ) {
    154 $self->{email_smtp_server} = '@EMAIL_SMTP_SERVER@';
    155 $self->{email_smtp_hello} = '@EMAIL_SMTP_HELLO@';
    156 $self->{email_smtp_timeout} = int(@EMAIL_SMTP_TIMEOUT@) || 10;
    157 $self->{email_smtp_ssl} = lc ('@EMAIL_SMTP_SSL@') eq 'yes' ? 1 : 0;
    158 $self->{email_smtp_port} = int(@EMAIL_SMTP_PORT@) || ($self->{email_smtp_ssl} ? 465 : 25);
    159 }
    160 }
    161
    147 162 $self->_refresh_();
    148 163 $self->_init_();
    149 164
     
    267 282 memcached_object_expire
    268 283 memcached_namespace
    269 284 convert_binary
    285
    286 email_enable email_mailer
    287 email_from email_auth_login email_auth_password
    288 email_smtp_server email_smtp_hello email_smtp_timeout email_smtp_ssl email_smtp_port
    270 289 ) )
    271 290 {
    272 291 $self->{attributes}->{ $attribute } = 'SCALAR';
  • utf8/core/project-default.mk

     
    65 65 RSYNC_DIRS ?=
    66 66 RSYNC_CORE_DIRS += contenido/i
    67 67
    68 EMAIL_ENABLE ?= NO
    69
    68 70 # apache pool
    69 71 ${PROJECT_LC}_START_SERVERS ?= 1
    70 72 START_SERVERS ?= ${${PROJECT_LC}_START_SERVERS}