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