We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    Hi!
    I'm refactoring some code I wrote using PHP's mail() function to use modPHPMailer and I need to be able to set the envelope MAIL FROM parameter. Before I did this with mail()'s fifth paramter, e.g. mail($to,$subject,$body,$headers, "-f [email protected]"), but I don't see a way to do this with modPHPMailer. I don't think it can be done in the headers. Any suggestions?
    Thanks!

    Dave


    Also, does modPHPMailer depend on sendmail?

    This question has been answered by BobRay. See the first response.

      • 40735
      • 119 Posts
      Anyone?
        • 3749
        • 24,544 Posts
        $modx->mail->set(modMail::MAIL_FROM_NAME, 'desiredFromName');



        I don't think it depends on sendMail(). I couldn't find any references to sendMail() anywhere in any of the relevant classes.

        FYI, many hosts have mail() turned off. You can use SMTP as an alternative by setting the SMTP System Settings in the Manager.

        If you install QuickEmail and run it in debug mode, it might tell you a little about what's happening. You can also use QuickEmail as a wrapper to send your mail, though it's probably not necessary.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 40735
          • 119 Posts
          Quote from: BobRay at Mar 26, 2013, 11:53 AM
          $modx->mail->set(modMail::MAIL_FROM_NAME, 'desiredFromName');



          I don't think it depends on sendMail(). I couldn't find any references to sendMail() anywhere in any of the relevant classes.

          FYI, many hosts have mail() turned off. You can use SMTP as an alternative by setting the SMTP System Settings in the Manager.

          If you install QuickEmail and run it in debug mode, it might tell you a little about what's happening. You can also use QuickEmail as a wrapper to send your mail, though it's probably not necessary.

          Hi Bob!
          Thanks for responding. MAIL_FROM_NAME only sets the "from" name in the email client. The address that the email is coming from still says [email protected] and the return-path is [email protected]. Why those two are different I don't know but I need to be able to set them both to a specific address. So far I can't see any way to do that. Also, the body of the email being sent contains some of the header information and the HTML content is being delivered in plain text despite the fact that I'm calling $modx->mail->setHTML(true);

          Thoughts?

          Thanks!
          • discuss.answer
            • 3749
            • 24,544 Posts
            Sorry, misread your post. Here's the code from the Login package with all of them:


            $this->modx->getService('mail', 'mail.modPHPMailer');
            $this->modx->mail->set(modMail::MAIL_BODY, $msg);
            $this->modx->mail->set(modMail::MAIL_FROM, $this->modx->getOption('emailsender'));
            $this->modx->mail->set(modMail::MAIL_FROM_NAME, $this->modx->getOption('site_name'));
            $this->modx->mail->set(modMail::MAIL_SENDER, $this->modx->getOption('emailsender'));
            $this->modx->mail->set(modMail::MAIL_SUBJECT, $subject);
            if (!empty($msgAlt)) {  // plaintext version
                $this->modx->mail->set(modMail::MAIL_BODY_TEXT, $msgAlt);
            }
            $this->modx->mail->address('to', $email, $name);
            $this->modx->mail->address('reply-to', $this->modx->getOption('emailsender'));
            $this->modx->mail->setHTML(true);
            $sent = $this->modx->mail->send();
            $this->modx->mail->reset();



            Your problem with setHTML is interesting. PhpStorm has always flagged that line as having a nonexistent class method in all my code, though the method appears to be there and it has always worked for me. I'm not sure what that means.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 40735
              • 119 Posts
              MAIL_SENDER did the trick! Thanks!

              Now if I can just figure out why the email isn't HTML and the headers are showing up in the body...
                • 3749
                • 24,544 Posts
                Here's some header code cribbed from SPForm:


                $this->_addlHeaders[] = "X-Remote-Host: " . $_SERVER['REMOTE_HOST'] . "\n";
                if ($this->modx->getOption('reportRemoteHost',$this->spfconfig,true) && !empty($_SERVER['REMOTE_HOST'])) {
                            $this->_addlHeaders[] = "X-Remote-Host: " . $_SERVER['REMOTE_HOST'] . "\n";
                        }
                        if ($this->modx->getOption('reportRemoteAddr',$this->spfconfig,true)) {
                            if(!empty($_SERVER['REMOTE_ADDR'])) {
                                $this->_addlHeaders[] = "X-Remote-Address: " . $_SERVER['REMOTE_ADDR'] . "\n";
                            }
                
                            /* If the client is working through a proxy... */
                            if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                                $this->_addlHeaders[] = "X-Http-X-Forwarded-For: " .
                                         $_SERVER['HTTP_X_FORWARDED_FOR'] . "\n";
                            }
                
                        }
                        if ($this->modx->getOption('reportRemoteUser',$this->spfconfig,true) && !empty($_SERVER['REMOTE_USER'])) {
                            $this->_addlHeaders[] = "X-Remote-User: " . $_SERVER['REMOTE_USER'] . "\n";
                        }
                
                foreach($this->_addlHeaders as $key => $value) {
                        /*  MS-Win mail servers want crlf and *don't* want a trailing pair
                         *  necessary only for addl headers */
                        if(PHP_OS == "WIN32" || PHP_OS == "WINNT") {
                            if (isset($value) && ! empty($value)) {
                               $this->_addlHeaders[$key] = preg_replace("/\n$/", "", $value);
                               $this->_addlHeaders[$key] = preg_replace("/\n/", "\r\n", $value);
                            }
                        }
                        $this->modx->mail->header($value);
                    }

                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 40735
                  • 119 Posts
                  It looks like the header() method is the culprit, or possibly what I'm setting for the headers. I was doing this:

                  $headers = "Bcc:".$bcc."\r\n".
                  	"MIME-Version: 1.0\r\n".
                  	"X-Mailer: PHP/".phpversion();
                  
                  $modx->getService("mail", "mail.modPHPMailer");
                  $modx->mail->set(modMail::MAIL_SENDER, $from);
                  $modx->mail->set(modMail::MAIL_FROM, $from);
                  $modx->mail->set(modMail::MAIL_FROM_NAME, $fromName);
                  $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
                  $modx->mail->set(modMail::MAIL_BODY, $body);
                  $modx->mail->address("to", $to);
                  $modx->mail->address("reply-to", "[email protected]");
                  $modx->mail->header($headers);
                  $modx->mail->setHTML(true);


                  I modified it to not use the header method and the problem went away:

                  $modx->getService("mail", "mail.modPHPMailer");
                  $modx->mail->set(modMail::MAIL_SENDER, $from);
                  $modx->mail->set(modMail::MAIL_FROM, $from);
                  $modx->mail->set(modMail::MAIL_FROM_NAME, $fromName);
                  $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
                  $modx->mail->set(modMail::MAIL_BODY, $body);
                  $modx->mail->address("to", $to);
                  $modx->mail->address("reply-to", "[email protected]");
                  $modx->mail->address("bcc", $bcc);
                  $modx->mail->setHTML(true);
                    • 3749
                    • 24,544 Posts
                    Try this:

                    $modx->mail->headers[] = 'desired header here';


                    The headers member is an array, so setting it to a string is likely to fail, and setting it to an array might overwrite something necessary. Also, it might not be part of the attributes array, which is where set() puts it.

                    It's public so the above might work. [ed. note: BobRay last edited this post 13 years, 6 months ago.]
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 40735
                      • 119 Posts
                      That definitely makes sense that it would fail then but the strange thing is that the BCC was still being set. That $headers string is the only place I was setting it before I changed it. I wonder if it was just because it was the first one in the string.