Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

send() failing without errors on shared hosting


Go to End
Reply

9 Posts   1994 Views

Avatar
Erin

10 March 2010 at 5:39am Community Member, 26 Posts

I'm failing to understand why my Silverstripe installation on shared hosting refuses to send emails, either from a custom function like this...
ContactPage.php

class ContactPage extends Page
{
   static $db = array(
      'Mailto' => 'Varchar(100)', //Email address to send submissions to
      'SubmitText' => 'HTMLText' //Text presented after submitting message
   );
   
   //CMS fields
   function getCMSFields() {
      $fields = parent::getCMSFields();
   
      $fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));   
      $fields->addFieldToTab("Root.Content.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));   
   
      return $fields;   
   }

}
class ContactPage_Controller extends Page_Controller
{
   function ContactForm() {
   // Create fields
   $Params = Director::urlParams();
      
    $fields = new FieldSet(
       new TextField('Name', 'Name*'),
         new EmailField('Email', 'Email*'),
         new TextareaField('Comments','Comments*')
      );
       
    // Create action
    $actions = new FieldSet(
       new FormAction('SendContactForm', 'Send')
    );
      // Create action
      $validator = new RequiredFields('Name', 'Email', 'Comments');

    return new Form($this, 'ContactForm', $fields, $actions, $validator);
   }

   function SendContactForm($data) {
       //Set data
      $From = $data['Email'];
      $To = $this->Mailto;
      $Subject = "Website Contact message";    
      $email = new Email($From, $To, $Subject);
      //set template
      $email->setTemplate('ContactEmail');
      //populate template
      $email->populateTemplate($data);
      //send mail
      $email->send();
       //return to submitted message
      Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
   }

   public function Success()
   {
      return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
   }
}


...or from a page created with the UserDefinedForm module. There are no errors logged and no errors displayed on the page--everything seems to check out, except that somewhere between the page and Email.php, the mail function just doesn't seem to work. I'm running PHP Version 5.2.13, with a configuration that works just fine when I write, outside of Silverstripe...

$email = $_POST['email'] ;
$message = $_POST['message'] ;
mail( "test@example.com", "Email Subject", $message, "From: $email" );
print "Email sent";

2+ years ago, someone recommended installing phpmailer to work around Silverstripe's native mail functioning. Is this STILL the best way to send emails from Silverstripe on a shared host???

Avatar
Erin

10 March 2010 at 8:42am Community Member, 26 Posts

In case anyone else is having the same problem, here's relevant code that's a little closer(?) to the problem:
sapphire/email/Mailer.php

   if(!($result = @mail($to, $subject, $fullBody, $headers, "-f$bounceAddress"))) {
      $result = mail($to, $subject, $fullBody, $headers);
   }


Sample message (raw) with variable names noted for clarity

test@example.com //$to
Sample Subject //$subject
This is a multi-part message in MIME format. //$fullBody

------=_NextPart_159628161319
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

I just really wanted to email you and say hi.

------=_NextPart_159628161319
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
=09<base href=3D"http://www.example.com/ss/" />
</head>
<body>

<p class=3D"body">
=09I just really wanted to email you and say hi.

</p>

</body>
</html>
------=_NextPart_159628161319--

MIME-Version: 1.0 //$headers
Content-Type: multipart/alternative; boundary="----=_NextPart_159628161319"
Content-Transfer-Encoding: 7bit
From: test@example.com
X-Mailer: SilverStripe Mailer - version 2006.06.21 (Sent from "www.example.com")
X-Priority: 3
X-SilverStripeBounceURL: www.example.com/ss/Email_BounceHandler
X-SilverStripeSite: mysite

Ideas? Anyone?

Avatar
Erin

11 March 2010 at 11:21am Community Member, 26 Posts

After much futzing around in the guts of Mailer.php and Email.php, I decided to change the recipient of my contact page emails to an address on my host, so that instead my script would try to send emails to admin@example.com instead of mypersonalemail@gmail.com. Emails which were subsequently attempted were generated and received, but I have no idea why.
Can anyone illuminate me?

Avatar
Juanitou

12 March 2010 at 2:17am Community Member, 323 Posts

Hi! I cannot enlighten you, since I had the same problem several months ago. I didn’t remembered what I did then, so I didn’t replied to your post. Now I remember! I’m going to check the wiki and try to write a line about this somewhere.

Best regards,
Juan

Avatar
Erin

12 March 2010 at 3:38am Community Member, 26 Posts

That's great. Could you let me know?

Avatar
Juanitou

12 March 2010 at 4:14am Community Member, 323 Posts

There’s nothing more to know: I seem to remember that as soon as I changed the address to one of the same domain, all started to work. I’m really sorry, but I can’t recall nothing else. I’ve checked my custom code for UserDefinedForm and the lines related to send() are not modified.

Avatar
Erin

12 March 2010 at 4:29am Community Member, 26 Posts

Understood. Thanks.

Avatar
UncleCheese

12 March 2010 at 4:38am 4085 Posts

You need to talk to your hosting provider. This happens all the time if your mail is hosted on a different server. The mailer looks at the destination domain and says, "oh, that's me.. I don't need to do anything".. so I never goes out to the internet to send the mail.

There's a setting somewhere in Apache that they can tweak to force the mailer to always look outside of its own DNS to send mail...

Go to Top