Lines 20-32
package C4::Letters;
Link Here
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use MIME::Lite; |
22 |
use MIME::Lite; |
23 |
use Mail::Sendmail; |
|
|
24 |
use Date::Calc qw( Add_Delta_Days ); |
23 |
use Date::Calc qw( Add_Delta_Days ); |
|
|
24 |
use Email::Sender::Simple qw(sendmail); |
25 |
use Encode; |
25 |
use Encode; |
26 |
use Carp; |
26 |
use Carp; |
27 |
use Template; |
27 |
use Template; |
28 |
use Module::Load::Conditional qw(can_load); |
28 |
use Module::Load::Conditional qw(can_load); |
29 |
|
29 |
|
|
|
30 |
use Try::Tiny; |
31 |
|
30 |
use C4::Members; |
32 |
use C4::Members; |
31 |
use C4::Log; |
33 |
use C4::Log; |
32 |
use C4::SMS; |
34 |
use C4::SMS; |
Lines 39-44
use Koha::Notice::Messages;
Link Here
|
39 |
use Koha::Notice::Templates; |
41 |
use Koha::Notice::Templates; |
40 |
use Koha::DateUtils qw( format_sqldatetime dt_from_string ); |
42 |
use Koha::DateUtils qw( format_sqldatetime dt_from_string ); |
41 |
use Koha::Patrons; |
43 |
use Koha::Patrons; |
|
|
44 |
use Koha::SMTP::Servers; |
42 |
use Koha::Subscriptions; |
45 |
use Koha::Subscriptions; |
43 |
|
46 |
|
44 |
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
47 |
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
Lines 338-364
sub SendAlerts {
Link Here
|
338 |
) or return; |
341 |
) or return; |
339 |
|
342 |
|
340 |
# ... then send mail |
343 |
# ... then send mail |
341 |
my $message = Koha::Email->new(); |
344 |
my $mail = Koha::Email->create( |
342 |
my %mail = $message->create_message_headers( |
|
|
343 |
{ |
345 |
{ |
344 |
to => $email, |
346 |
to => $email, |
345 |
from => $library->branchemail, |
347 |
from => $library->branchemail, |
346 |
replyto => $library->branchreplyto, |
348 |
replyto => $library->branchreplyto, |
347 |
sender => $library->branchreturnpath, |
349 |
sender => $library->branchreturnpath, |
348 |
subject => Encode::encode( "UTF-8", "" . $letter->{title} ), |
350 |
subject => Encode::encode( "UTF-8", "" . $letter->{title} ), |
349 |
message => $letter->{'is_html'} |
|
|
350 |
? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ), |
351 |
Encode::encode( "UTF-8", "" . $letter->{'title'} )) |
352 |
: Encode::encode( "UTF-8", "" . $letter->{'content'} ), |
353 |
contenttype => $letter->{'is_html'} |
354 |
? 'text/html; charset="utf-8"' |
355 |
: 'text/plain; charset="utf-8"', |
356 |
} |
351 |
} |
357 |
); |
352 |
); |
358 |
unless( Mail::Sendmail::sendmail(%mail) ) { |
353 |
|
359 |
carp $Mail::Sendmail::error; |
354 |
if ( $letter->{is_html} ) { |
360 |
return { error => $Mail::Sendmail::error }; |
355 |
$mail->html_body( |
|
|
356 |
_wrap_html( |
357 |
Encode::encode( "UTF-8", $letter->{content} ), |
358 |
Encode::encode( "UTF-8", "" . $letter->{title} ) |
359 |
) |
360 |
); |
361 |
} |
361 |
} |
|
|
362 |
else { |
363 |
$mail->text_body( |
364 |
Encode::encode( "UTF-8", "" . $letter->{'content'} ) |
365 |
); |
366 |
} |
367 |
|
368 |
try { |
369 |
my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); |
370 |
Email::Sender::Simple::sendmail( $mail, { transport => $smtp_server->transport } ); |
371 |
} |
372 |
catch { |
373 |
carp "$_"; |
374 |
return { error => "$_" }; |
375 |
}; |
362 |
} |
376 |
} |
363 |
} |
377 |
} |
364 |
elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) { |
378 |
elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) { |
Lines 477-484
sub SendAlerts {
Link Here
|
477 |
|
491 |
|
478 |
# ... then send mail |
492 |
# ... then send mail |
479 |
my $library = Koha::Libraries->find( $userenv->{branch} ); |
493 |
my $library = Koha::Libraries->find( $userenv->{branch} ); |
480 |
my $email = Koha::Email->new(); |
494 |
my $mail = Koha::Email->create( |
481 |
my %mail = $email->create_message_headers( |
|
|
482 |
{ |
495 |
{ |
483 |
to => join( ',', @email ), |
496 |
to => join( ',', @email ), |
484 |
cc => join( ',', @cc ), |
497 |
cc => join( ',', @cc ), |
Lines 487-514
sub SendAlerts {
Link Here
|
487 |
C4::Context->preference("ClaimsBccCopy") |
500 |
C4::Context->preference("ClaimsBccCopy") |
488 |
&& ( $type eq 'claimacquisition' |
501 |
&& ( $type eq 'claimacquisition' |
489 |
|| $type eq 'claimissues' ) |
502 |
|| $type eq 'claimissues' ) |
490 |
) ? ( bcc => $userenv->{emailaddress} ) |
503 |
) |
|
|
504 |
? ( bcc => $userenv->{emailaddress} ) |
491 |
: () |
505 |
: () |
492 |
), |
506 |
), |
493 |
from => $library->branchemail |
507 |
from => $library->branchemail |
494 |
|| C4::Context->preference('KohaAdminEmailAddress'), |
508 |
|| C4::Context->preference('KohaAdminEmailAddress'), |
495 |
subject => Encode::encode( "UTF-8", "" . $letter->{title} ), |
509 |
subject => Encode::encode( "UTF-8", "" . $letter->{title} ), |
496 |
message => $letter->{'is_html'} ? _wrap_html( |
|
|
497 |
Encode::encode( "UTF-8", $letter->{'content'} ), |
498 |
Encode::encode( "UTF-8", "" . $letter->{'title'} ) |
499 |
) |
500 |
: Encode::encode( "UTF-8", "" . $letter->{'content'} ), |
501 |
contenttype => $letter->{'is_html'} |
502 |
? 'text/html; charset="utf-8"' |
503 |
: 'text/plain; charset="utf-8"', |
504 |
} |
510 |
} |
505 |
); |
511 |
); |
506 |
|
512 |
|
507 |
unless ( Mail::Sendmail::sendmail(%mail) ) { |
513 |
if ( $letter->{is_html} ) { |
508 |
carp $Mail::Sendmail::error; |
514 |
$mail->html_body( |
509 |
return { error => $Mail::Sendmail::error }; |
515 |
_wrap_html( |
|
|
516 |
Encode::encode( "UTF-8", $letter->{'content'} ), |
517 |
Encode::encode( "UTF-8", "" . $letter->{'title'} ) |
518 |
) |
519 |
); |
520 |
} |
521 |
else { |
522 |
$mail->text_body( |
523 |
Encode::encode( "UTF-8", "" . $letter->{'content'} ) ); |
510 |
} |
524 |
} |
511 |
|
525 |
|
|
|
526 |
try { |
527 |
my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); |
528 |
Email::Sender::Simple::sendmail( $mail, { transport => $smtp_server->transport } ); |
529 |
} |
530 |
catch { |
531 |
carp "$_"; |
532 |
return { error => "$_" }; |
533 |
}; |
534 |
|
512 |
logaction( |
535 |
logaction( |
513 |
"ACQUISITION", |
536 |
"ACQUISITION", |
514 |
$action, |
537 |
$action, |
Lines 523-549
sub SendAlerts {
Link Here
|
523 |
} |
546 |
} |
524 |
# send an "account details" notice to a newly created user |
547 |
# send an "account details" notice to a newly created user |
525 |
elsif ( $type eq 'members' ) { |
548 |
elsif ( $type eq 'members' ) { |
526 |
my $library = Koha::Libraries->find( $externalid->{branchcode} )->unblessed; |
549 |
my $library = Koha::Libraries->find( $externalid->{branchcode} ); |
527 |
my $letter = GetPreparedLetter ( |
550 |
my $letter = GetPreparedLetter ( |
528 |
module => 'members', |
551 |
module => 'members', |
529 |
letter_code => $letter_code, |
552 |
letter_code => $letter_code, |
530 |
branchcode => $externalid->{'branchcode'}, |
553 |
branchcode => $externalid->{'branchcode'}, |
531 |
lang => $externalid->{lang} || 'default', |
554 |
lang => $externalid->{lang} || 'default', |
532 |
tables => { |
555 |
tables => { |
533 |
'branches' => $library, |
556 |
'branches' => $library->unblessed, |
534 |
'borrowers' => $externalid->{'borrowernumber'}, |
557 |
'borrowers' => $externalid->{'borrowernumber'}, |
535 |
}, |
558 |
}, |
536 |
substitute => { 'borrowers.password' => $externalid->{'password'} }, |
559 |
substitute => { 'borrowers.password' => $externalid->{'password'} }, |
537 |
want_librarian => 1, |
560 |
want_librarian => 1, |
538 |
) or return; |
561 |
) or return; |
539 |
return { error => "no_email" } unless $externalid->{'emailaddr'}; |
562 |
return { error => "no_email" } unless $externalid->{'emailaddr'}; |
540 |
my $email = Koha::Email->new(); |
563 |
my $mail = Koha::Email->create( |
541 |
my %mail = $email->create_message_headers( |
|
|
542 |
{ |
564 |
{ |
543 |
to => $externalid->{'emailaddr'}, |
565 |
to => $externalid->{'emailaddr'}, |
544 |
from => $library->{branchemail}, |
566 |
from => $library->branchemail, |
545 |
replyto => $library->{branchreplyto}, |
567 |
replyto => $library->branchreplyto, |
546 |
sender => $library->{branchreturnpath}, |
568 |
sender => $library->branchreturnpath, |
547 |
subject => Encode::encode( "UTF-8", "" . $letter->{'title'} ), |
569 |
subject => Encode::encode( "UTF-8", "" . $letter->{'title'} ), |
548 |
message => $letter->{'is_html'} |
570 |
message => $letter->{'is_html'} |
549 |
? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ), |
571 |
? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ), |
Lines 554-563
sub SendAlerts {
Link Here
|
554 |
: 'text/plain; charset="utf-8"', |
576 |
: 'text/plain; charset="utf-8"', |
555 |
} |
577 |
} |
556 |
); |
578 |
); |
557 |
unless( Mail::Sendmail::sendmail(%mail) ) { |
579 |
try { |
558 |
carp $Mail::Sendmail::error; |
580 |
my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); |
559 |
return { error => $Mail::Sendmail::error }; |
581 |
Email::Sender::Simple::sendmail( $mail, { transport => $smtp_server->transport } ); |
560 |
} |
582 |
} |
|
|
583 |
catch { |
584 |
carp "$_"; |
585 |
return { error => "$_" }; |
586 |
}; |
561 |
} |
587 |
} |
562 |
|
588 |
|
563 |
# If we come here, return an OK status |
589 |
# If we come here, return an OK status |
Lines 1296-1312
sub _send_message_by_email {
Link Here
|
1296 |
my $content = encode('UTF-8', $message->{'content'}); |
1322 |
my $content = encode('UTF-8', $message->{'content'}); |
1297 |
my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"'; |
1323 |
my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"'; |
1298 |
my $is_html = $content_type =~ m/html/io; |
1324 |
my $is_html = $content_type =~ m/html/io; |
|
|
1325 |
|
1299 |
my $branch_email = undef; |
1326 |
my $branch_email = undef; |
1300 |
my $branch_replyto = undef; |
1327 |
my $branch_replyto = undef; |
1301 |
my $branch_returnpath = undef; |
1328 |
my $branch_returnpath = undef; |
|
|
1329 |
my $library; |
1330 |
|
1302 |
if ($patron) { |
1331 |
if ($patron) { |
1303 |
my $library = $patron->library; |
1332 |
$library = $patron->library; |
1304 |
$branch_email = $library->branchemail; |
1333 |
$branch_email = $library->branchemail; |
1305 |
$branch_replyto = $library->branchreplyto; |
1334 |
$branch_replyto = $library->branchreplyto; |
1306 |
$branch_returnpath = $library->branchreturnpath; |
1335 |
$branch_returnpath = $library->branchreturnpath; |
1307 |
} |
1336 |
} |
1308 |
my $email = Koha::Email->new(); |
1337 |
|
1309 |
my %sendmail_params = $email->create_message_headers( |
1338 |
my $email = Koha::Email->create( |
1310 |
{ |
1339 |
{ |
1311 |
to => $to_address, |
1340 |
to => $to_address, |
1312 |
( |
1341 |
( |
Lines 1314-1342
sub _send_message_by_email {
Link Here
|
1314 |
? ( bcc => C4::Context->preference('NoticeBcc') ) |
1343 |
? ( bcc => C4::Context->preference('NoticeBcc') ) |
1315 |
: () |
1344 |
: () |
1316 |
), |
1345 |
), |
1317 |
from => $message->{'from_address'} || $branch_email, |
1346 |
from => $message->{'from_address'} || $branch_email, |
1318 |
replyto => $message->{'reply_address'} || $branch_replyto, |
1347 |
replyto => $message->{'reply_address'} || $branch_replyto, |
1319 |
sender => $branch_returnpath, |
1348 |
sender => $branch_returnpath, |
1320 |
subject => $subject, |
1349 |
subject => $subject |
1321 |
message => $is_html ? _wrap_html( $content, $subject ) : $content, |
|
|
1322 |
contenttype => $content_type |
1323 |
} |
1350 |
} |
1324 |
); |
1351 |
); |
1325 |
|
1352 |
|
1326 |
$sendmail_params{'Auth'} = {user => $username, pass => $password, method => $method} if $username; |
1353 |
if ( $is_html ) { |
|
|
1354 |
$email->html_body( |
1355 |
_wrap_html( $content, $subject ) |
1356 |
); |
1357 |
} |
1358 |
else { |
1359 |
$email->text_body( $content ); |
1360 |
} |
1361 |
|
1362 |
my $smtp_server; |
1363 |
if ( $library ) { |
1364 |
$smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); |
1365 |
} |
1366 |
else { |
1367 |
$smtp_server = Koha::SMTP::Servers->get_default; |
1368 |
} |
1327 |
|
1369 |
|
1328 |
_update_message_to_address($message->{'message_id'},$sendmail_params{To}) if !$message->{to_address} || $message->{to_address} ne $sendmail_params{To}; #if initial message address was empty, coming here means that a to address was found and queue should be updated; same if to address was overriden by create_message_headers |
1370 |
if ( $username ) { |
|
|
1371 |
$smtp_server->set( |
1372 |
{ |
1373 |
sasl_username => $username, |
1374 |
sasl_password => $password, |
1375 |
} |
1376 |
); |
1377 |
} |
1329 |
|
1378 |
|
1330 |
if ( Mail::Sendmail::sendmail( %sendmail_params ) ) { |
1379 |
# if initial message address was empty, coming here means that a to address was found and |
1331 |
_set_message_status( { message_id => $message->{'message_id'}, |
1380 |
# queue should be updated; same if to address was overriden by create_message_headers |
1332 |
status => 'sent' } ); |
1381 |
_update_message_to_address( $message->{'message_id'}, $email->email->header('To') ) |
|
|
1382 |
if !$message->{to_address} |
1383 |
|| $message->{to_address} ne $email->email->header('To'); |
1384 |
|
1385 |
try { |
1386 |
Email::Sender::Simple::sendmail( $email, { transport => $smtp_server->transport } ); |
1387 |
|
1388 |
_set_message_status( |
1389 |
{ |
1390 |
message_id => $message->{'message_id'}, |
1391 |
status => 'sent' |
1392 |
} |
1393 |
); |
1333 |
return 1; |
1394 |
return 1; |
1334 |
} else { |
|
|
1335 |
_set_message_status( { message_id => $message->{'message_id'}, |
1336 |
status => 'failed' } ); |
1337 |
carp $Mail::Sendmail::error; |
1338 |
return; |
1339 |
} |
1395 |
} |
|
|
1396 |
catch { |
1397 |
_set_message_status( |
1398 |
{ |
1399 |
message_id => $message->{'message_id'}, |
1400 |
status => 'failed' |
1401 |
} |
1402 |
); |
1403 |
carp "$_"; |
1404 |
return; |
1405 |
}; |
1340 |
} |
1406 |
} |
1341 |
|
1407 |
|
1342 |
sub _wrap_html { |
1408 |
sub _wrap_html { |