splitbrain / dokuwiki-plugin-smtp

Send DokuWiki mails via a configured SMTP server
https://www.dokuwiki.org/plugin:smtp
12 stars 14 forks source link

Empty mail body #17

Closed tutench closed 4 years ago

tutench commented 7 years ago

When I create a new user the DokuWiki sents out a mail with the password info but the mail body is completely empty... The same happens when I get a notification about a changed article.

Release 2016-06-26a "Elenor of Tsort" SMTP Server is an Exchange Server 2013

With the SwiftMailer Plugin everything works fine

If more information is needed, let me know.

Japanuspus commented 7 years ago

I am seeing the same issue: Using the SMTP plugin I will get an email, but the body is empty, both when using the SMTP test page in the admin panel, and when using any other email feature (password reset, etc.).

I am using an in-house exchange server with no authorization. The same server is also mentioned in this forum thread where the problem also has been reported and analyzed: https://forum.dokuwiki.org/post/56370). In the forum thread, the problem was resolved by changing email server, but this option is not open to me.

I can also reproduce the debug trail from the forum thread. This indicates that the advise_before-hook called on line 689 of Mailer.class.php does not return true: Modifying this file as follows:

// do our thing if BEFORE hook approves
$evt = new Doku_Event('MAIL_MESSAGE_SEND', $data);
echo "<hr>J1 - Before Hook ".$this->text;
if($evt->advise_before(true)) {
    echo "<hr>J2 - Inside hook".$this->text;
    // clean up before using the headers
    $this->cleanHeaders();
    . . .
}
echo "<hr>J3 - After Hook ".$this->text;

I get the following output when trying to send email (from admin test panel):

<hr>J1 - Before Hook Hi jawes

This is a test from http://csi00774/
-- 
This mail was generated by DokuWiki at
http://csi00774/
<hr>J3 - After Hook Hi jawes

This is a test from http://csi00774/
-- 
This mail was generated by DokuWiki at
http://csi00774/

Note that J2 is missing. Please let me know if there is anything I can do to debug.

SMTP plugin, Installed version: 2017-02-08 DokuWiki: Release 2017-02-19b "Frusterick Manners"

jkbha commented 7 years ago

This seems to be a bug in the way Exchange server is interpreting the mail. Apparently, (some) Exchange servers is not happy with a single end of line character between header and body, but requires TWO (see e.g. https://www.hmailserver.com/forum/viewtopic.php?t=7456 ).

While this is most likely a problem on the Exchange server's side, it should be easy to fix in the next update of the plugin by including an extra blank line between header and body.

pierpower commented 7 years ago

Hello, I've looked into the issue and found that, after the "Content-Transfer-Encoding: base64" string, there is no carriage return (\r), but only 2 line feed chars (\n).

So, I've added the missing carriage return with the following patch in the "toString()" function inside classes\Message.php:

    public function toString() {
    // we need to remove the BCC header here
    $lines = explode("\n", $this->body);
    $count = count($lines);
    for($i=0; $i<$count; $i++) {
        if(trim($lines[$i]) === '') break; // end of headers, we're done
        if(substr($lines[$i],0, 4) == 'Bcc:') {
            unset($lines[$i]); // we found the Bcc: header and remove it
            while(substr($lines[++$i],0, 1) === ' ') {
                unset($lines[$i]); // indented lines are header continuiation
            }
            break; // header removed, we're done
        }
    }
   ////-------------------------------PATCH START--------------------------------////
    //need to add a carriage return after "base64"
    for($i=0; $i<$count; $i++) {
        $pos = strpos($lines[$i],'base64');
        if($pos!=FALSE) {
            $lines[$i] .= "\r"; 
            }
    }
    ////-------------------------------PATCH END--------------------------------////

    $body = join("\n", $lines);

    return $body . $this->CRLF . $this->CRLF . "." . $this->CRLF;
}

I'm not sure if this is the best way to do that, but it works on my installation.

SMTP server : Exchange server. SMTP plugin, Installed version: 2017-02-08 DokuWiki: Release 2017-02-19b "Frusterick Manners"

Hope that helps.

Netand1 commented 7 years ago

Ok UNIX problem ..you need to put : \r in line 88 ( $body = join("\r\n", $lines);) of /site/lib/plugins/smtp/classes/message.php

lpagie commented 6 years ago

I followed suggestions in https://forum.dokuwiki.org/thread/14686 which solved the issue for me. Also adds a blank line after the header but in slightly different way. from thread:

A hacky workaround is to include a line like :
$body = str_replace("base64", "base64\r\n\r\n", $body);

in the smpt/action.php right after the $body is generated
$body = $mailer->dump();

I hope hope this can help in fixing the plugin (I couldn't easily figure out how to fix code on github)

grts

TVetrovsky commented 6 years ago

The suggested fixes did not worked for me. Configuration: IIS, PHP 7.2.5, DokuWiki 2018-04-22a "Greebo", SMTP Plugin 2017-08-03, Exchange mail server

Previous fix mentioned by @Netand1 worked for plain text emails but not for HTML emails. There are likely more issues with new line encoding. My fix was: /site/lib/plugins/smtp/classes/message.php - toString function:

public function toString() {
        // we need to remove the BCC header here
        $body = str_replace("\r\n", "\n", $this->body); // added line
        $lines = explode("\n", $body); // changed line
        $count = count($lines);
        for($i=0; $i<$count; $i++) {
            if(trim($lines[$i]) === '') break; // end of headers, we're done
            if(substr($lines[$i],0, 4) == 'Bcc:') {
                unset($lines[$i]); // we found the Bcc: header and remove it
                while(substr($lines[++$i],0, 1) === ' ') {
                    unset($lines[$i]); // indented lines are header continuiation
                }
                break; // header removed, we're done
            }
        }
        $body = join("\r\n", $lines); // fix suggested by @Netand1

        return $body . $this->CRLF . $this->CRLF . "." . $this->CRLF;
    }

This fix works both for plaintext and html emails.

rawIce commented 5 years ago

Ok UNIX problem ..you need to put : \r in line 88 ( $body = join("\r\n", $lines);) of /site/lib/plugins/smtp/classes/message.php

that thing also worked for me, simply add "\r" in the line specified.

Schroeffu commented 5 years ago

Had the same issue, your workaround https://github.com/splitbrain/dokuwiki-plugin-smtp/issues/17#issuecomment-433044021 works for me too.

In my case the file is uppercase (Message.php), so (path_to_dokuwiki_htdocs)/lib/plugins/smtp/classes/Message.php

NiyaShy commented 4 years ago

Can confirm that adding /r on line 88 of Message.php fixes the empty mail body when using the plugin with Exchange (in our case, Exchange 2019). Works for both plain text and HTML mails.