Welcome Guest, Not a member yet? Register   Sign In
php mail() works, CI email library doesn't: how to configure correctly?
#1

[eluser]w1ntermut3[/eluser]
Hi,

I should prefix this with: the mail/dns setup on the server is not ideal. It's a dedicated Windows box at a well-known - and godawful - uk hosting company. It's got one SMTP server installed by the control panel software on port 25, which is locked down for relaying and about as documented as an undocumented thing, and IIS's default SMTP server on port 26, which by rights is open to anything at all, although I'm b*ggered if I can get a peep out of it. Tech support at said hosting company seems to be staffed by surly teenagers who can just about read the "we don't support that" script in front of them.

In addition, I'm hosting just the client site on the server: their MX records are still pointing at... wherever they were. We've not touched them: we're not allowed to touch them. Given our mail setup, you can probably appreciate why.

So. I want to send an email from [email protected]

This works:

Code:
ini_set('smtp_port', '25');
mail('[email protected]', 'Email Test 1', 'Email Test 1',
    "To: [email protected]\n" .  
    "From: [email protected]\n" .  
    "MIME-Version: 1.0\n" .  
    "Content-type: text/html; charset=iso-8859-1");

This doesn't:

Code:
$mail_config['protocol']    = 'smtp';
$mail_config['smtp_host']    = 'localhost';
$mail_config['smtp_port']    = 25;
$mail_config['smtp_timeout']    = 10;
$this->load->library('email', $mail_config);
$this->email->from('[email protected]', 'Website');
$this->email->to('[email protected]');
$this->email->subject('Email Test 2');
$this->email->message('Email Test 2');
$this->email->send();
echo $this->email->print_debugger();

My question is: what's different between the two, and how do I make the second one match the first?

Obviously the smtp host stands out - I've tried everything I can think of in there: full root domain, IP address, runic incantations. I keep returning this debug error:

Quote:220 ****** SMTP Mail Server v(1.4) on Simple Mail Transfer Service Ready

hello: 250 SIZE 10485760

from: 250 OK

to: 551 Bad Recipient

The following SMTP error was encountered: 551 Bad Recipient

data: 451 Requested action aborted: No Valid Recipients

The following SMTP error was encountered: 451 Requested action aborted: No Valid Recipients
500 Syntax Error, Command Unrecognized User-Agent: CodeIgniter
The following SMTP error was encountered: 500 Syntax Error, Command Unrecognized User-Agent: CodeIgniter
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

Anyone have a better idea than me what's going on here? I mean, obviously there ARE; there are probably small fish with a better understanding of mail in PHP than me, but any and all advice would be appreciated.

Thanks.
#2

[eluser]RiggsFolly[/eluser]
I am having my own problems in this area but i did notice that you missed the

Code:
$this->email->initialize($config);

after setting your $config variables and before doing all the to and from stuff.

I think the order should be

Code:
$this->load->library( 'email' );
$mail_config['protocol'] = 'smtp';
... etc
$this->email->initialize($mail_config);
#3

[eluser]RiggsFolly[/eluser]
Hi,

I managed to solve my problems. I have successfully sent email via SMTP and CI_Mail.

CI Version 1.7.2
OS Windows XP SP3
WAMP Server 2.0i
- Apache 2.2.11
- PHP 5.3.1
- MySQL 5.1.36
hMailServer 5.3.2-B1769 running on separate PC on internal network.


I am using a mailserver on my internal network called hMailServer. Its very good as a local testing mailserver.

However my CI application would hang after receiving the first 220 <server name> message from the mail server and then eventually timing out after max php execution time and giving me NO IDEA what was wrong.

I eventually found that it would only work with a "\r\n" for the newline and crlf parameters.
If your problem is a "fast idle" in other words your page hangs as if downloading a HUGE page, this could be what your problem is.

The class defines
Code:
var $newline = "\n";    // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
var $crlf = "\n";    // The RFC 2045 compliant CRLF for quoted-printable is "\r\n".  Apparently some servers,
                     // even on the receiving end think they need to muck with CRLFs, so using "\n", while
                     // distasteful, is the only thing that seems to work for all environments.


So i used these parameters coded in my controller and all seemed to start working as if by magic.

Code:
$mail_config['protocol']    = 'smtp';
$mail_config['smtp_host']    = 'obscured.webhop.net';   // mailserver on laptop
$mail_config['smtp_user']    = '[email protected]';
$mail_config['smtp_pass']    = 'xxxxxx';
$mail_config['smtp_port']    = '25';
$mail_config['smtp_timeout'] = 5;
$mail_config['charset']      = 'utf-8';
$mail_config['mailtype']    = 'text';
$mail_config['crlf']        = "\r\n";        // CHANGED FROM DEFAULTS
$mail_config['newline']     = "\r\n";        // CHANGED FROM DEFAULTS
$mail_config['wrapchars']   = 76;
$mail_config['wordwrap']    = TRUE;

$this->email->initialize($mail_config);

$this->email->from( $this->input->post( 'email' ), $this->input->post( 'name' ) );
$this->email->to('[email protected]');
$this->email->cc('[email protected]');        
$this->email->subject( $this->input->post( 'subject' ) );
$this->email->message( $this->input->post( 'msg' ) );

$this->email->send();


I hope some of this will point you to your problem.

But suffice to say, it does work when you get all the environment/parameters correct.
#4

[eluser]DanSearle[/eluser]
thanks for that, the \r\n trick worked for me, I just added this to my email.php config file and it works great.

Code:
$config['crlf']        = "\r\n";        // CHANGED FROM DEFAULTS
$config['newline']     = "\r\n";        // CHANGED FROM DEFAULTS




Theme © iAndrew 2016 - Forum software by © MyBB