CodeIgniter Forums
Mail sending problems. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Mail sending problems. (/showthread.php?tid=10349)

Pages: 1 2 3


Mail sending problems. - El Forum - 07-28-2008

[eluser]Adam Griffiths[/eluser]
My model...

Code:
<?php

class Email_model extends Model
{
    function __construct()
    {
        parent::Model();
    }
    
    function index()
    {
        echo("You cannot access this page directly");
    }
    
    function quick($name, $email, $subject, $message)
    {
            $this->email->from($email, $name);
            $this->email->reply_to($email, $name);
            $this->email->to('[email protected]');

            $this->email->subject($subject);
            $this->email->message($message);

            $this->email->send();
            
            echo $this->email->print_debugger();
    }
}

/* End of file email.php */
/* Location: system/application/models/ */

My controller...

Code:
<?php

class Email extends Controller
{
    function __construct()
    {
        parent::Controller();
        $this->load->helper('security');
        $this->load->helper('email');
    }
    
    function index()
    {
        echo("You cannot access this page directly");
    }
    
    function quick()
    {
        if($_POST['quickform'])
        {
            $name = xss_clean($_POST['name']);
            $email = xss_clean($_POST['email']);
            $subject = xss_clean($_POST['subject']);
            $message = xss_clean($_POST['message']);
            
            if($name | $email | $subject | $message == NULL)
            {
                $data['title'] = 'Quick Enquiry';
                $this->load->view('header', $data);
                $this->load->view('form/quick_posterror');
                $this->load->view('footer');
            }
            elseif(valid_email($email))
            {
                $this->load->model('email_model');
                
                $this->email_model->quick($name, $email, $subject, $message);

                $data['title'] = 'Quick Enquiry';
                $this->load->view('header', $data);
                $this->load->view('form/success');
                $this->load->view('footer');
            }
            else
            {
                $data['title'] = 'Quick Enquiry';
                $this->load->view('header', $data);
                $this->load->view('form/invalid_email');
                $this->load->view('footer');
            }
        }
        else
        {
            $data['title'] = 'Quick Enquiry';
            $this->load->view('header', $data);
            $this->load->view('form/quick_posterror');
            $this->load->view('footer');
        }
    }
}

/* End of file email.php */
/* Location: system/application/controllers/ */

The form in my view...

Code:
</div>

<div id="sidebar">
    
    <div class="box">
    <p class="head">Quick Enquiry Form</p>
    <p>&lt;?php echo form_open('email/quick');?&gt;</p>
    <p class="head2">Name:
    &lt;input type="text" name="name" class="quickform" /&gt;&lt;/p>
    <p class="head2">Email:
    &lt;input type="text" name="email" class="quickform" /&gt;&lt;/p>
    <p class="head2">Subject:
    &lt;input type="text" name="subject" class="quickform" /&gt;&lt;/p>
    <p class="head2">Message:
    &lt;textarea class="quickformbig" rows="0" cols="0" name="message"&gt;&lt;/textarea></p>
    <p>&lt;input type="submit" name="quickform" class="send" /&gt;&lt;/p>
    &lt;/form&gt;
    </div>
    
    </div>
    
    <div id="footer">
    <div id="copyright">
    <div id="text1">&copy; Nanoswift MMVIII</div>
    </div>
    </div>
    
&lt;/body&gt;
&lt;/html&gt;

I get this from the debugger in the email class.

Quote:Your message has been successfully sent using the following protocol: mail

From: "Adam Griffiths"
Return-Path:
Reply-To: "Adam Griffiths"
X-Sender: EMAIL EDITED OUT IN CASE OF SPAM BOTS =]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
test again
another test

So I know everything I put into the form is coming out the other end in the model etc. But the email doesn't get to my mailbox. Junk goes into my inbox anyway, so it isn't that the mail() function is blocked. I've tried it on two accounts. Can anybody see the problem? This is my first time using the email library so I'm a total newb with it.

Thanks in advance!


Mail sending problems. - El Forum - 07-28-2008

[eluser]Nathan Moore[/eluser]
Try using a different mail protocol and see if that helps.

For example, try adding these lines before sending the email:

Code:
$config['protocol'] = 'sendmail';
$this->email->initialize($config);

The default protocol is mail (php)... Try sendmail and if that does not work, try using SMTP.


Just as a sidenote, you should probably send the email from the controller, not the model. Only use the model to interact with the database.


Mail sending problems. - El Forum - 07-28-2008

[eluser]Adam Griffiths[/eluser]
When I changed the protocol to sendmail, I get 3 PHP notices and none of the data gets passed to the model at all.

There is a live version up at Nanoswift it's the form at the side.


Mail sending problems. - El Forum - 07-28-2008

[eluser]Nathan Moore[/eluser]
It looks like the variables are not being set. Where did you place the initialize code?

Try to put it before you set the from, reply-to, to, etc.


Mail sending problems. - El Forum - 07-28-2008

[eluser]Adam Griffiths[/eluser]
[quote author="Nathan Moore" date="1217285312"]It looks like the variables are not being set. Where did you place the initialize code?

Try to put it before you set the from, reply-to, to, etc.[/quote]

Here is my new model.

Code:
&lt;?php

class Email_model extends Model
{
    function __construct()
    {
        parent::Model();
    }
    
    function index()
    {
        echo("You cannot access this page directly");
    }
    
    function quick($name, $email, $subject, $message)
    {
            $config['protocol'] = 'sendmail';
            $this->email->initialize($config);
            
            $this->email->from($email, $name);
            $this->email->reply_to($email, $name);
            $this->email->to('[email protected]');

            $this->email->subject($subject);
            $this->email->message($message);
            
            $this->email->send();
            
            echo $this->email->print_debugger();
    }
}

/* End of file email.php */
/* Location: system/application/models/ */

The debugger says this...

Quote:Your message has been successfully sent using the following protocol: sendmail

User-Agent: CodeIgniter
Date: Mon, 28 Jul 2008 18:44:08 +0000
From: "Adam Griffiths"
Return-Path:
Reply-To: "Adam Griffiths"
To: xxxxxxxxxxxxxxxxxxxx
Subject: Technology Jokes
X-Sender: xxxxxxxxxxxxxxxxxxxxxx
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

this is not a joke

But once again the email has not been sent. I tried it with two emails and they both didn't get the email.

Edit: I echo'ed the variables from inside the model, and they are the same as in the form...

Quote:Name: Adam Griffiths
Email: xxxxxxxxxxxxxxxxx
Subject: Technology Jokes
Message: this is not a joke
Your message has been successfully sent using the following protocol: sendmail

User-Agent: CodeIgniter
Date: Mon, 28 Jul 2008 18:47:02 +0000
From: "Adam Griffiths"
Return-Path:
Reply-To: "Adam Griffiths"
To: xxxxxxxxxxxxxxxxxxxx
Subject: Technology Jokes
X-Sender: xxxxxxxxxxxxxxxxxxxxx
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

this is not a joke

I edited out my emails again.


Mail sending problems. - El Forum - 07-28-2008

[eluser]johnwbaxter[/eluser]
Are you using plesk on your server?


Mail sending problems. - El Forum - 07-28-2008

[eluser]Adam Griffiths[/eluser]
[quote author="audiopleb" date="1217292178"]Are you using plesk on your server?[/quote]

No, I'm on servage so it's the Servage CP.


Mail sending problems. - El Forum - 07-28-2008

[eluser]johnwbaxter[/eluser]
Well that's one thing to rule out then. Have you been able to send any e-mails out from the server at all? Other domains, CPanel mails perhaps (but sent from your server)?


Mail sending problems. - El Forum - 07-28-2008

[eluser]Adam Griffiths[/eluser]
I just used an old contact form I have in the same directory as the CI install, and it works fine. It uses mail().


Mail sending problems. - El Forum - 07-28-2008

[eluser]johnwbaxter[/eluser]
Ah good. I just wanted to narrow down the possible reasons. I'll have a thorough look at this tomorrow.