Doug Molineux
Doug Molineux

Reputation: 12431

CodeIgniter PHP Mail Function Not Working

I have a script which sends an email to myself from a contact page:

if($_POST["submit"] == "Send Message")
{
    $to = "[email protected]";
    $subject = "Message received from Contact Us";
    $message = "Email: ".$_POST["email"]."<br>";
    $message .= "Name: ".$_POST["name"]."<br>";
    $message .= "Message: ".$_POST["message"]."<br>";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    echo mail($to,$subject,$message,$headers);
    $messageSent = TRUE;
}

It does actually echo 1. But I am not receiving any emails in my email account (gmail). I am using Codeigniter I saw there's an email library, but this should work shouldn't it?

I have a couple of other similar forms, I don't really want to integrate this library if I already have normal PHP to do it.

I know that my server is capable of sending emails because I've done it before, i have a feeling this is codeigniter related. If there is no other options, I suppose I can use the library and change the code. Any advice on this will help! Thank you :)

Upvotes: 0

Views: 8226

Answers (2)

Ross
Ross

Reputation: 17967

its not CI related as you aren't making use of any CI functionality.

any reason you ~aren't~ using the email helper?

in CI:

$this->load->library('form_validation');

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('message', 'Message', 'required');

if($this->form_validation->run() == FALSE)
{
    $this->load->view('contact-form');
}
else
{
    $this->load->library('email');

    $name = $this->input->post('name');
    $email = $this->input->post('email');
   $message= $this->input->post('message');

    $this->email->from($email, $name);
    $this->email->to('[email protected]');

    $this->email->subject('Subject');
    $this->email->message($message);
    if($this->email->send())
    {
        echo $this->email->print_debugger();
        //redirect('contact-us/thanks', 'location');
    }
   else
   {
    echo 'Something went wrong...';
   }

}

also note that mail returns true/false depending on if php was able to SEND It. there's no way of telling if the message was ever received.

try it in CI - see if there is any difference in outcome

Upvotes: 2

ITroubs
ITroubs

Reputation: 11215

Codeigniter can't be responsible for your problems because mail() is a php function which can't be overwritten except by some fancy string preprocessing before including a php source file.

Upvotes: 0

Related Questions