Ignore:
Timestamp:
Jul 27, 2011 11:44:06 AM (22 months ago)
Author:
thomasb
Message:

Move mail sending functions from mail task to core for general usage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/main.inc

    r4963 r4977  
    16241624 
    16251625/** 
     1626 * Send the given message using the configured method 
     1627 * 
     1628 * @param object $message    Reference to Mail_MIME object 
     1629 * @param string $from       Sender address string 
     1630 * @param array  $mailto     Array of recipient address strings 
     1631 * @param array  $smtp_error SMTP error array (reference) 
     1632 * @param string $body_file  Location of file with saved message body (reference) 
     1633 * @param array  $smtp_opts  SMTP options (e.g. DSN request) 
     1634 * 
     1635 * @return boolean Send status. 
     1636 */ 
     1637function rcmail_deliver_message(&$message, $from, $mailto, &$smtp_error, &$body_file, $smtp_opts=null) 
     1638{ 
     1639  global $CONFIG, $RCMAIL; 
     1640 
     1641  $headers = $message->headers(); 
     1642 
     1643  // send thru SMTP server using custom SMTP library 
     1644  if ($CONFIG['smtp_server']) { 
     1645    // generate list of recipients 
     1646    $a_recipients = array($mailto); 
     1647 
     1648    if (strlen($headers['Cc'])) 
     1649      $a_recipients[] = $headers['Cc']; 
     1650    if (strlen($headers['Bcc'])) 
     1651      $a_recipients[] = $headers['Bcc']; 
     1652 
     1653    // clean Bcc from header for recipients 
     1654    $send_headers = $headers; 
     1655    unset($send_headers['Bcc']); 
     1656    // here too, it because txtHeaders() below use $message->_headers not only $send_headers 
     1657    unset($message->_headers['Bcc']); 
     1658 
     1659    $smtp_headers = $message->txtHeaders($send_headers, true); 
     1660 
     1661    if ($message->getParam('delay_file_io')) { 
     1662      // use common temp dir 
     1663      $temp_dir = $RCMAIL->config->get('temp_dir'); 
     1664      $body_file = tempnam($temp_dir, 'rcmMsg'); 
     1665      if (PEAR::isError($mime_result = $message->saveMessageBody($body_file))) { 
     1666        raise_error(array('code' => 600, 'type' => 'php', 
     1667            'file' => __FILE__, 'line' => __LINE__, 
     1668            'message' => "Could not create message: ".$mime_result->getMessage()), 
     1669            TRUE, FALSE); 
     1670        return false; 
     1671      } 
     1672      $msg_body = fopen($body_file, 'r'); 
     1673    } else { 
     1674      $msg_body = $message->get(); 
     1675    } 
     1676 
     1677    // send message 
     1678    if (!is_object($RCMAIL->smtp)) 
     1679      $RCMAIL->smtp_init(true); 
     1680 
     1681    $sent = $RCMAIL->smtp->send_mail($from, $a_recipients, $smtp_headers, $msg_body, $smtp_opts); 
     1682    $smtp_response = $RCMAIL->smtp->get_response(); 
     1683    $smtp_error = $RCMAIL->smtp->get_error(); 
     1684 
     1685    // log error 
     1686    if (!$sent) 
     1687      raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__, 
     1688                        'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE); 
     1689  } 
     1690  // send mail using PHP's mail() function 
     1691  else { 
     1692    // unset some headers because they will be added by the mail() function 
     1693    $headers_enc = $message->headers($headers); 
     1694    $headers_php = $message->_headers; 
     1695    unset($headers_php['To'], $headers_php['Subject']); 
     1696 
     1697    // reset stored headers and overwrite 
     1698    $message->_headers = array(); 
     1699    $header_str = $message->txtHeaders($headers_php); 
     1700 
     1701    // #1485779 
     1702    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { 
     1703      if (preg_match_all('/<([^@]+@[^>]+)>/', $headers_enc['To'], $m)) { 
     1704        $headers_enc['To'] = implode(', ', $m[1]); 
     1705      } 
     1706    } 
     1707 
     1708    $msg_body = $message->get(); 
     1709 
     1710    if (PEAR::isError($msg_body)) 
     1711      raise_error(array('code' => 600, 'type' => 'php', 
     1712            'file' => __FILE__, 'line' => __LINE__, 
     1713            'message' => "Could not create message: ".$msg_body->getMessage()), 
     1714            TRUE, FALSE); 
     1715    else { 
     1716      $delim   = $RCMAIL->config->header_delimiter(); 
     1717      $to      = $headers_enc['To']; 
     1718      $subject = $headers_enc['Subject']; 
     1719      $header_str = rtrim($header_str); 
     1720 
     1721      if ($delim != "\r\n") { 
     1722        $header_str = str_replace("\r\n", $delim, $header_str); 
     1723        $msg_body   = str_replace("\r\n", $delim, $msg_body); 
     1724        $to         = str_replace("\r\n", $delim, $to); 
     1725        $subject    = str_replace("\r\n", $delim, $subject); 
     1726      } 
     1727 
     1728      if (ini_get('safe_mode')) 
     1729        $sent = mail($to, $subject, $msg_body, $header_str); 
     1730      else 
     1731        $sent = mail($to, $subject, $msg_body, $header_str, "-f$from"); 
     1732    } 
     1733  } 
     1734 
     1735  if ($sent) { 
     1736    $RCMAIL->plugins->exec_hook('message_sent', array('headers' => $headers, 'body' => $msg_body)); 
     1737 
     1738    // remove MDN headers after sending 
     1739    unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']); 
     1740 
     1741    // get all recipients 
     1742    if ($headers['Cc']) 
     1743      $mailto .= $headers['Cc']; 
     1744    if ($headers['Bcc']) 
     1745      $mailto .= $headers['Bcc']; 
     1746    if (preg_match_all('/<([^@]+@[^>]+)>/', $mailto, $m)) 
     1747      $mailto = implode(', ', array_unique($m[1])); 
     1748 
     1749    if ($CONFIG['smtp_log']) { 
     1750      write_log('sendmail', sprintf("User %s [%s]; Message for %s; %s", 
     1751        $RCMAIL->user->get_username(), 
     1752        $_SERVER['REMOTE_ADDR'], 
     1753        $mailto, 
     1754        !empty($smtp_response) ? join('; ', $smtp_response) : '')); 
     1755    } 
     1756  } 
     1757 
     1758  if (is_resource($msg_body)) { 
     1759    fclose($msg_body); 
     1760  } 
     1761 
     1762  $message->_headers = array(); 
     1763  $message->headers($headers); 
     1764 
     1765  return $sent; 
     1766} 
     1767 
     1768 
     1769// Returns unique Message-ID 
     1770function rcmail_gen_message_id() 
     1771{ 
     1772  global $RCMAIL; 
     1773 
     1774  $local_part  = md5(uniqid('rcmail'.mt_rand(),true)); 
     1775  $domain_part = $RCMAIL->user->get_username('domain'); 
     1776 
     1777  // Try to find FQDN, some spamfilters doesn't like 'localhost' (#1486924) 
     1778  if (!preg_match('/\.[a-z]+$/i', $domain_part)) { 
     1779    if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST'])) 
     1780      && preg_match('/\.[a-z]+$/i', $host)) { 
     1781        $domain_part = $host; 
     1782    } 
     1783    else if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['SERVER_NAME'])) 
     1784      && preg_match('/\.[a-z]+$/i', $host)) { 
     1785        $domain_part = $host; 
     1786    } 
     1787  } 
     1788 
     1789  return sprintf('<%s@%s>', $local_part, $domain_part); 
     1790} 
     1791 
     1792 
     1793// Returns RFC2822 formatted current date in user's timezone 
     1794function rcmail_user_date() 
     1795{ 
     1796  global $CONFIG; 
     1797 
     1798  // get user's timezone 
     1799  if ($CONFIG['timezone'] === 'auto') { 
     1800    $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : date('Z')/3600; 
     1801  } 
     1802  else { 
     1803    $tz = $CONFIG['timezone']; 
     1804    if ($CONFIG['dst_active']) 
     1805      $tz++; 
     1806  } 
     1807 
     1808  $date = time() + $tz * 60 * 60; 
     1809  $date = gmdate('r', $date); 
     1810  $tz   = sprintf('%+05d', intval($tz) * 100 + ($tz - intval($tz)) * 60); 
     1811  $date = preg_replace('/[+-][0-9]{4}$/', $tz, $date); 
     1812 
     1813  return $date; 
     1814} 
     1815 
     1816 
     1817/** 
    16261818 * Check if working in SSL mode 
    16271819 * 
Note: See TracChangeset for help on using the changeset viewer.