add sendmail parameters

Hello,

 

I work on my first elgg project. 

 

My sendmail app need somme spécific params to work. 

But i Can't edit my php.ini file. 

My first (and only solution), is to hack the elgg code :'(

 

engine/lib/notification.php elgg_send_email()

return mail($to, $subject, wordwrap($body), $headers);

replaced by

return mail($to, $subject, wordwrap($body), $headers, ' -f elgg@foo.fr'); // HACK : ajout de ' -f elgg@foo.fr'

 

I'm looking for a best solution. 

 

Thanks for your help :)

  • If you look at elgg_send_email()

    There is a hook you can use (I think it's 'email', 'system') - and there you can send the email using mail() with the 5th parameter and return true in your hook to let the function know you've handled it.  This will allow you to implement your solution in a plugin and not hack the core.

  • good :) 

    I do not know the hook system. 

     

  • http://docs.elgg.org/wiki/Plugin_Hooks

    I assume you know about the plugin skeleton, start.php and the init function - if not you'll need to read up on that in the docs

     

    in your init function it'll be something like

    elgg_register_plugin_hook_handler('email', 'system', 'myplugin_mailer');

     

    Then you'll define your new function

    function myplugin_mailer($hook, $type, $return, $params) {

      // $params will have all of the info you need
      send your email using mail()

      return true; // returning true will stop the core mail() call

    }

  • <?php
    /**
     * mod/myplugin/start.php
     *
     * @package myplugin
     *
     */
    
    elgg_register_event_handler('init', 'system', 'myplugin_init');
    
    /**
     * Init myplugin plugin.
    */
    function myplugin_init() {
        elgg_register_plugin_hook_handler('email', 'system', 'myplugin_system_email');
    }
    
    
    /**
     * Function appelé pour l'envoie de mail. 
     * 
     *
     * === engine/lib/notification.php ===
     * === elgg_send_email() ===
     *
     * <code>
     *	     $mail_params = array(
     *	                    'to' => $to,
     *	                    'from' => $from,
     *	                    'subject' => $subject,
     *	                    'body' => $body,
     *	                    'params' => $params
     *	    );
     *
     *	    $result = elgg_trigger_plugin_hook('email', 'system', $mail_params, NULL);
     * 
     *
     * @param string $hook
     * @param string $type
     * @param mixed  $return
     * @param array  $params = array('to' => $to,'from' => $from,'subject' => $subject,'body' => $body,'params' => $params);
     * @return unknown|boolean
     */
    function myplugin_system_email($hook, $type, $return, $params)
    {
        global $CONFIG;
    
        $to = $params['to'];
        $from = $params['from'];
        $subject = $params['subject'];
        $body = $params['body'];
        // $params = $params['params']; // Optional parameters (none used in this function)
    
        ///////////////////////////////////// START CopCol de elgg_send_email() L.323
        $header_eol = "\r\n";
        if (isset($CONFIG->broken_mta) && $CONFIG->broken_mta) {
            // Allow non-RFC 2822 mail headers to support some broken MTAs
            $header_eol = "\n";
        }
    
        // Windows is somewhat broken, so we use just address for to and from
        if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
            // strip name from to and from
            if (strpos($to, '<')) {
                preg_match('/<(.*)>/', $to, $matches);
                $to = $matches[1];
            }
            if (strpos($from, '<')) {
                preg_match('/<(.*)>/', $from, $matches);
                $from = $matches[1];
            }
        }
    
        $headers = "From: $from{$header_eol}"
        . "Content-Type: text/plain; charset=UTF-8; format=flowed{$header_eol}"
        . "MIME-Version: 1.0{$header_eol}"
        . "Content-Transfer-Encoding: 8bit{$header_eol}";
    
    
        // Sanitise subject by stripping line endings
        $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject);
        // this is because Elgg encodes everything and matches what is done with body
        $subject = html_entity_decode($subject, ENT_COMPAT, 'UTF-8'); // Decode any html entities
        if (is_callable('mb_encode_mimeheader')) {
            $subject = mb_encode_mimeheader($subject, "UTF-8", "B");
        }
    
        // Format message
        $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8'); // Decode any html entities
        $body = elgg_strip_tags($body); // Strip tags from message
        $body = preg_replace("/(\r\n|\r)/", "\n", $body); // Convert to unix line endings in body
        $body = preg_replace("/^From/", ">From", $body); // Change lines starting with From to >From
        ///////////////////////////////////// STOP CopCol de elgg_send_email() L.361
        
        mail($to, $subject, wordwrap($body), $headers, ' -f elgg@myplugin.fr'); // ajout de ' -f elgg@myplugin.fr'
    
        return true; // returning true will stop the core mail() call
    }
    ?>
    
  • good job buddy.but i have one question,it is safe for user

  • I have no idea. Why then does he not be?

    It's just a hook that does 'exactely' the default functionality.