Changeset 25344 for trunk/include/functions_mail.inc.php
- Timestamp:
- Nov 5, 2013, 8:23:29 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_mail.inc.php
r25018 r25344 22 22 // +-----------------------------------------------------------------------+ 23 23 24 // +-----------------------------------------------------------------------+ 25 // | functions | 26 // +-----------------------------------------------------------------------+ 27 28 /* 29 * Returns the name of the mail sender : 30 * 24 /** 25 * Returns the name of the mail sender 31 26 * @return string 32 27 */ … … 38 33 } 39 34 40 /* 35 /** 36 * Returns the email of the mail sender 37 * @since 2.6 38 * @return string 39 */ 40 function get_mail_sender_email() 41 { 42 global $conf; 43 44 return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']); 45 } 46 47 /** 41 48 * Returns an array of mail configuration parameters : 42 *43 * - mail_options44 49 * - send_bcc_mail_webmaster 45 * - default_email_format 46 * - alternative_email_format 50 * - allow_html_email 47 51 * - use_smtp 48 52 * - smtp_host 49 53 * - smtp_user 50 54 * - smtp_password 51 * - boundary_key55 * - smtp_secure 52 56 * - email_webmaster 53 * - formated_email_webmaster57 * - name_webmaster 54 58 * 55 59 * @return array … … 60 64 61 65 $conf_mail = array( 62 'mail_options' => $conf['mail_options'],63 66 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], 64 ' default_email_format' => $conf['default_email_format'],65 ' alternative_email_format' => $conf['alternative_email_format'],67 'allow_html_email' => $conf['allow_html_email'], 68 'mail_theme' => $conf['mail_theme'], 66 69 'use_smtp' => !empty($conf['smtp_host']), 67 70 'smtp_host' => $conf['smtp_host'], … … 69 72 'smtp_password' => $conf['smtp_password'], 70 73 'smtp_secure' => $conf['smtp_secure'], 74 'email_webmaster' => get_mail_sender_email(), 75 'name_webmaster' => get_mail_sender_name(), 71 76 ); 72 77 73 // we have webmaster id among user list, what's his email address ?74 $conf_mail['email_webmaster'] = get_webmaster_mail_address();75 76 // name of the webmaster is the title of the gallery77 $conf_mail['formated_email_webmaster'] = format_email(get_mail_sender_name(), $conf_mail['email_webmaster']);78 79 78 return $conf_mail; 80 79 } … … 82 81 /** 83 82 * Returns an email address with an associated real name 84 *85 83 * @param string name 86 84 * @param string email … … 88 86 function format_email($name, $email) 89 87 { 90 // Spring cleaning91 88 $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); 92 89 $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); … … 108 105 109 106 /** 107 * Returns the mail and the name from a formatted address 108 * @since 2.6 109 * @param string|array $input 110 * @return array 111 */ 112 function unformat_email($input) 113 { 114 if (is_array($input)) 115 { 116 return $input; 117 } 118 119 if (preg_match('/(.*)<(.*)>.*/', $input, $matches)) 120 { 121 return array( 122 'email' => trim($matches[2]), 123 'name' => trim($matches[1]), 124 ); 125 } 126 else 127 { 128 return array( 129 'email' => trim($input), 130 'name' => '', 131 ); 132 } 133 } 134 135 136 /** 110 137 * Returns an email address list with minimal email string 111 * 112 * @ param string with email list (email separated by comma)138 * @param string $email_list - comma separated 139 * @return string 113 140 */ 114 141 function get_strict_email_list($email_list) … … 116 143 $result = array(); 117 144 $list = explode(',', $email_list); 145 118 146 foreach ($list as $email) 119 147 { … … 131 159 /** 132 160 * Return an new mail template 133 * 134 * @param string email_format: mail format, text/html or text/plain 135 * @param string theme: theme to use [default get_default_theme()] 136 */ 137 function & get_mail_template($email_format, $theme='') 138 { 139 if (empty($theme)) 140 { 141 $theme = get_default_theme(); 142 } 143 144 $mail_template = new Template(PHPWG_ROOT_PATH.'themes', $theme, 'template/mail/'.$email_format); 145 146 return $mail_template; 147 } 148 149 /** 150 * Return string email format (text/html or text/plain) 151 * 152 * @param string format 153 */ 154 function get_str_email_format($is_html) 155 { 156 return ($is_html ? 'text/html' : 'text/plain'); 157 } 158 159 /* 160 * Switch language to param language 161 * @param string $email_format - text/html or text/plain 162 * @return Template 163 */ 164 function &get_mail_template($email_format) 165 { 166 $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format); 167 return $template; 168 } 169 170 /** 171 * Switch language to specified language 161 172 * All entries are push on language stack 162 * 163 * @param string language 173 * @param string $language 164 174 */ 165 175 function switch_lang_to($language) … … 198 208 load_language('admin.lang', '', array('language'=>$language) ); 199 209 210 // Reload all plugins files (see load_language declaration) 200 211 if (!empty($language_files)) 201 212 { … … 220 231 } 221 232 222 /* 233 /** 223 234 * Switch back language pushed with switch_lang_to function 224 *225 * @param: none226 235 */ 227 236 function switch_lang_back() … … 457 466 } 458 467 459 /* 468 /** 460 469 * sends an email, using Piwigo specific informations 461 470 * 462 * @param: 463 * - to: receiver(s) of the mail (list separated by comma). 464 * - args: function params of mail function: 471 * @param string|string[] $to 472 * @param array $args 465 473 * o from: sender [default value webmaster email] 466 474 * o Cc: array of carbon copy receivers of the mail. [default value empty] … … 470 478 * o content_format: format of mail content [default value 'text/plain'] 471 479 * o email_format: global mail format [default value $conf_mail['default_email_format']] 472 * o theme: template to use [default get_default_theme()] 480 * o theme: theme to use [default value $conf_mail['mail_theme']] 481 * o mail_title: main title of the mail [default value $conf['gallery_title']] 482 * o mail_subtitle: subtitle of the mail [default value subject] 473 483 * 474 * @return boolean (Ok or not)484 * @return boolean 475 485 */ 476 486 function pwg_mail($to, $args = array()) … … 492 502 $mail = new PHPMailer; 493 503 494 foreach (explode(',', get_strict_email_list($to)) as $recipient) 495 { 496 $mail->addAddress($recipient); 497 } 498 504 $recipients = !is_array($to) ? explode(',', $to) : $to; 505 foreach ($recipients as $recipient) 506 { 507 $recipient = unformat_email($recipient); 508 $mail->addAddress($recipient['email'], $recipient['name']); 509 } 510 499 511 $mail->WordWrap = 76; 500 512 $mail->CharSet = 'UTF-8'; … … 505 517 if (empty($args['from'])) 506 518 { 507 $ mail->From = get_webmaster_mail_address();508 $mail->FromName = get_mail_sender_name();509 510 $mail->addReplyTo(get_webmaster_mail_address(), get_mail_sender_name());519 $from = array( 520 'email' => $conf_mail['email_webmaster'], 521 'name' => $conf_mail['name_webmaster'], 522 ); 511 523 } 512 524 else 513 525 { 514 $mail->From = $args['from']; 515 $mail->addReplyTo($args['from']); 516 } 526 $from = unformat_email($args['from']); 527 } 528 $mail->setFrom($from['email'], $from['name']); 529 $mail->addReplyTo($from['email'], $from['name']); 517 530 518 531 // Subject … … 521 534 $args['subject'] = 'Piwigo'; 522 535 } 523 524 536 $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); 525 526 537 $mail->Subject = $args['subject']; 527 538 … … 529 540 if (!empty($args['Cc'])) 530 541 { 531 $mail->addCC($args['Cc']); 542 foreach ($args['Cc'] as $cc) 543 { 544 $cc = unformat_email($cc); 545 $mail->addCC($cc['email'], $cc['name']); 546 } 532 547 } 533 548 … … 535 550 if ($conf_mail['send_bcc_mail_webmaster']) 536 551 { 537 $args['Bcc'][] = get_webmaster_mail_address();; 538 } 539 552 $args['Bcc'][] = get_webmaster_mail_address(); 553 } 540 554 if (!empty($args['Bcc'])) 541 555 { 542 556 foreach ($args['Bcc'] as $bcc) 543 557 { 544 $mail->addBCC($bcc); 545 } 558 $bcc = unformat_email($bcc); 559 $mail->addBCC($bcc['email'], $bcc['name']); 560 } 561 } 562 563 // theme 564 if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark'))) 565 { 566 $args['theme'] = $conf_mail['mail_theme']; 546 567 } 547 568 548 569 // content 549 if (empty($args['email_format']))550 {551 $args['email_format'] = $conf_mail['default_email_format'];552 }553 554 570 if (!isset($args['content'])) 555 571 { 556 572 $args['content'] = ''; 557 573 } 558 574 if (!isset($args['mail_title'])) 575 { 576 $args['mail_title'] = $conf['gallery_title']; 577 } 578 if (!isset($args['mail_subtitle'])) 579 { 580 $args['mail_subtitle'] = $args['subject']; 581 } 582 583 // content type 559 584 if (empty($args['content_format'])) 560 585 { … … 562 587 } 563 588 564 if (empty($args['theme'])) 565 { 566 $args['theme'] = get_default_theme(); 567 } 568 569 $content_type_list[] = $args['email_format']; 570 if (!empty($conf_mail['alternative_email_format'])) 571 { 572 $content_type_list[] = $conf_mail['alternative_email_format']; 573 } 589 $content_type_list = array(); 590 if ($conf_mail['allow_html_email'] and @$args['email_format'] != 'text/plain') 591 { 592 $content_type_list[] = 'text/html'; 593 } 594 $content_type_list[] = 'text/plain'; 574 595 575 596 $contents = array(); 576 577 foreach (array_unique($content_type_list) as $content_type)578 {579 // key compose of indexes witch allow ti cache mail data580 $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme'];597 foreach ($content_type_list as $content_type) 598 { 599 // key compose of indexes witch allow to cache mail data 600 $cache_key = $content_type.'-'.$lang_info['code']; 601 $cache_key.= '-'.crc32(@$args['mail_title'] . @$args['mail_subtitle']); 581 602 582 603 if (!isset($conf_mail[$cache_key])) 583 604 { 605 // instanciate a new Template 584 606 if (!isset($conf_mail[$cache_key]['theme'])) 585 607 { 586 $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']); 608 $conf_mail[$cache_key]['theme'] = get_mail_template($content_type); 609 trigger_action('before_parse_mail_template', $cache_key, $content_type); 587 610 } 588 611 … … 595 618 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], 596 619 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', 597 'PHPWG_URL' => PHPWG_URL, 598 'TITLE_MAIL' => urlencode(l10n('A comment on your site')), 599 'MAIL' => get_webmaster_mail_address() 620 'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '', 621 'CONTENT_ENCODING' => get_pwg_charset(), 622 'CONTACT_MAIL' => $conf_mail['email_webmaster'], 623 'MAIL_TITLE' => $args['mail_title'], 624 'MAIL_SUBTITLE' => $args['mail_subtitle'], 600 625 ) 601 626 ); … … 609 634 } 610 635 611 $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl'; 612 if (is_file($file)) 636 if ($conf_mail[$cache_key]['theme']->smarty->template_exists('mail-css-'. $args['theme'] .'.tpl')) 613 637 { 614 $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file));638 $conf_mail[$cache_key]['theme']->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl'); 615 639 $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css'); 616 640 } 617 641 } 618 642 619 // what are displayed on the header of each mail ?620 643 $conf_mail[$cache_key]['header'] = $conf_mail[$cache_key]['theme']->parse('mail_header', true); 621 622 // what are displayed on the footer of each mail ?623 644 $conf_mail[$cache_key]['footer'] = $conf_mail[$cache_key]['theme']->parse('mail_footer', true); 624 645 } … … 628 649 629 650 // Content 630 if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html')) 631 { 632 $contents[$content_type].= '<p>'. 633 nl2br( 634 preg_replace("/(http:\/\/)([^\s,]*)/i", 635 "<a href='$1$2' class='thumblnk'>$1$2</a>", 636 htmlspecialchars($args['content']))). 637 '</p>'; 638 } 639 else if (($args['content_format'] == 'text/html') and ($content_type == 'text/plain')) 651 if ($args['content_format'] == 'text/plain' and $content_type == 'text/html') 652 { 653 // convert plain text to html 654 $contents[$content_type].= 655 '<p>'. 656 nl2br( 657 preg_replace( 658 '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i', 659 '<a href="$1">$1</a>', 660 htmlspecialchars($args['content']) 661 ) 662 ). 663 '</p>'; 664 } 665 else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain') 640 666 { 641 667 // convert html text to plain text … … 654 680 unset_make_full_url(); 655 681 682 // Send content to PHPMailer 656 683 if (isset($contents['text/html'])) 657 684 { 658 $mail->isHTML(true); // Set email format to HTML659 $mail->Body = $contents['text/html'];685 $mail->isHTML(true); 686 $mail->Body = move_css_to_body($contents['text/html']); 660 687 661 688 if (isset($contents['text/plain'])) … … 704 731 } 705 732 706 $ret = $mail->send(); 707 if(!$ret) 708 { 709 trigger_error( 'Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING); 710 } 733 $ret = true; 734 $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail); 735 736 if ($pre_result) 737 { 738 $ret = $mail->send(); 739 if (!$ret and is_admin()) 740 { 741 trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING); 742 } 743 } 744 711 745 return $ret; 712 746 } 713 747 714 /* DEPRECATED 715 * pwg sendmail 716 * 717 * @param: 718 * - result of other sendmail 719 * - to: Receiver or receiver(s) of the mail. 720 * - subject [default value 'Piwigo'] 721 * - content: content of mail 722 * - headers: headers of mail 723 * 724 * @return boolean (Ok or not) 725 */ 726 function pwg_send_mail($result, $to, $subject, $contents, $headers) 727 { 748 /** 749 * @deprecated 2.6 750 */ 751 function pwg_send_mail($result, $to, $subject, $content, $headers) 752 { 753 trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE); 754 728 755 if (!$result) 729 756 { 730 include_once(PHPWG_ROOT_PATH.'include/class.phpmailer.php'); 757 return pwg_mail($to, array( 758 'content' => $content, 759 'subject' => $subject, 760 )); 761 } 762 else 763 { 764 return $result; 765 } 766 } 767 768 /** 769 * @deprecated 2.6 770 */ 771 function move_ccs_rules_to_body($content) 772 { 773 trigger_error('move_ccs_rules_to_body function is deprecated, use move_css_to_body', E_USER_NOTICE); 774 775 return move_css_to_body($content); 776 } 777 778 /** 779 * Moves CSS rules contained in the <style> tag to inline CSS 780 * (for compatibility with Gmail and such clients) 781 * @since 2.6 782 * @param string $content 783 * @return string 784 */ 785 function move_css_to_body($content) 786 { 787 include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php'); 788 789 $e = new Emogrifier($content); 790 $e->preserveStyleTag = true; 791 return $e->emogrify(); 792 } 793 794 /** 795 * Saves a copy of the mail if _data/tmp 796 * @param boolean $result 797 * @param string $to 798 * @param array $args 799 * @param PHPMailer $mail 800 * @return boolean $result 801 */ 802 function pwg_send_mail_test($result, $to, $args, $mail) 803 { 804 global $conf, $user, $lang_info; 805 806 $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; 807 if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) 808 { 809 $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); 810 if ($args['content_format'] == 'text/plain') 811 { 812 $filename .= '.txt'; 813 } 814 else 815 { 816 $filename .= '.html'; 817 } 731 818 732 global $conf_mail; 733 734 if ($conf_mail['use_smtp']) 735 { 736 include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); 737 $smtp_mail = new smtp_mail( 738 $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], 739 $conf_mail['email_webmaster']); 740 return $smtp_mail->mail($to, $subject, $content, $headers); 741 } 742 else 743 { 744 $mail = new PHPMailer; 745 746 $mail->From = 'plg@pigolabs.com'; 747 $mail->FromName = 'Pierrick en local'; 748 foreach (explode(',', $to) as $recipient) 749 { 750 $mail->addAddress($recipient); // Add a recipient 751 } 752 // $mail->addReplyTo('plg@piwigo.org', 'Pierrick de Piwigo.org'); 753 // $mail->addCC('cc@example.com'); 754 // $mail->addBCC('bcc@example.com'); 755 756 $mail->WordWrap = 76; // Set word wrap to 50 characters 757 758 if (isset($contents['text/html'])) 759 { 760 $mail->isHTML(true); // Set email format to HTML 761 $mail->Body = $contents['text/html']; 762 763 if (isset($contents['text/plain'])) 764 { 765 $mail->AltBody = $contents['text/plain']; 766 } 767 } 768 else 769 { 770 $mail->isHTML(false); 771 $mail->Body = $contents['text/plain']; 772 } 773 774 $mail->CharSet = 'UTF-8'; 775 $mail->Subject = $subject; 776 777 if(!$mail->send()) { 778 echo 'Message could not be sent.'; 779 echo 'Mailer Error: ' . $mail->ErrorInfo; 780 exit; 781 } 782 783 // if ($conf_mail['mail_options']) 784 // { 785 // $options = '-f '.$conf_mail['email_webmaster']; 786 // return mail($to, $subject, $content, $headers, $options); 787 // } 788 // else 789 // { 790 // return mail($to, $subject, $content, $headers); 791 // } 792 } 793 } 794 else 795 { 796 return $result; 797 } 798 } 799 800 function move_ccs_rules_to_body($content) 801 { 802 return $content; 803 // We search all css rules in style tags 804 preg_match('#<style>(.*?)</style>#s', $content, $matches); 805 806 if (!empty($matches[1])) 807 { 808 preg_match_all('#([^\n]*?)\{(.*?)\}#s', $matches[1], $matches); 809 810 $selectors = array(); 811 $unknow_selectors = ''; 812 813 foreach ($matches[1] as $key => $value) 814 { 815 $selects = explode(',', $value); 816 $style = trim($matches[2][$key], ' ;'); 817 818 foreach($selects as $select) 819 { 820 $select = trim($select); 821 $selectors[$select][] = $style; 822 } 823 } 824 825 foreach ($selectors as $selector => $style) 826 { 827 if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches)) 828 { 829 $unknow_selectors .= $selector.' {'.implode(";\n", $style).";}\n"; 830 } 831 else switch ($matches[1]) 832 { 833 case '#': 834 $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); 835 break; 836 case '.': 837 $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); 838 break; 839 default: 840 $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode(";\n", $style).";\"\n$1", $content); 841 break; 842 } 843 } 844 845 // Keep unknow tags in page head 846 if (!empty($unknow_selectors)) 847 { 848 $content = preg_replace('#<style>.*?</style>#s', "<style type=\"text/css\">\n$unknow_selectors</style>", $content); 849 } 850 else 851 { 852 $content = preg_replace('#<style>.*?</style>#s', '', $content); 853 } 854 } 855 return $content; 856 } 857 858 /*Testing block*/ 859 function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) 860 { 861 global $conf, $user, $lang_info; 862 $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; 863 if ( mkgetdir( $dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) ) 864 { 865 $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); 866 if ($args['content_format'] == 'text/plain') 867 { 868 $filename .= '.txt'; 869 } 870 else 871 { 872 $filename .= '.html'; 873 } 874 $file = fopen($filename, 'w+'); 875 fwrite($file, $to ."\n"); 876 fwrite($file, $subject ."\n"); 877 fwrite($file, $headers); 878 fwrite($file, $content); 879 fclose($file); 880 } 881 return $result; 882 } 819 $file = fopen($filename, 'w+'); 820 fwrite($file, implode(', ', $to) ."\n"); 821 fwrite($file, $mail->Subject ."\n"); 822 fwrite($file, $mail->createHeader() ."\n"); 823 fwrite($file, $mail->createBody()); 824 fclose($file); 825 } 826 827 return $result; 828 } 829 883 830 if ($conf['debug_mail']) 884 add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6); 885 886 887 add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); 888 add_event_handler('send_mail_content', 'move_ccs_rules_to_body'); 831 { 832 add_event_handler('before_send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 4); 833 } 834 889 835 trigger_action('functions_mail_included'); 890 836
Note: See TracChangeset
for help on using the changeset viewer.