Welcome Guest, Not a member yet? Register   Sign In
sending a basic mail with php mail()
#1

[eluser]virtualgadjo[/eluser]
Hi all,

not exactly new to CI but this time i feel like a newb Smile

i'm desesperatly trying to send a simple mail from a website using CI email library (with protocol => mail) and i get the following error
Quote:Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
when sending the same mail the "old" way with the basic
Code:
$headers  = "From: ".utf8_decode($from)."\n";
$headers .= 'Reply-To: '.$data['email']."\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$success  = @mail($to, stripslashes(utf8_decode($sujet)), stripslashes(utf8_decode($body)), $headers)
it works like a breeze !!! (then i can assume my server is configured to send mail with mail() can't i ? Smile)

any idea would be warmly welcome Smile
chris

PS sorry, forgot to say CI 2.0.1...
#2

[eluser]virtualgadjo[/eluser]
hi again,

worked it out, probably in a dirty way but i'm eager to hear from you
i've changed line 1540 in the email libray

Code:
if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))

instead of

Code:
if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))

and it works like a charm now...
to be precise, safe_mode if off on my server and set on FALSE in the email library
this line occurs if it's on FALSE, changing it into the same line as it it were on TRUE and everything works again, there is porbably something i haven't understood but... it works !
something else, the comment above the line speaks about sendmail... when we are in mail() mode, may be that could be a hint for you guys but sincerely i don't know, i never use sendmail

hope it helps (and, nevermind, still eager to hear about a better/cleaner solution if there is one...)

Have swing
#3

[eluser]kenjis[/eluser]
Your server is safe mode, isn't it?

You can check by phpinfo() or ini_get('safe_mode').
#4

[eluser]virtualgadjo[/eluser]
hi Kenji,

no, it isn't, i've checked before the modification but, actually, i knew it was not because it's one of the first things i check, this, magic_quotes, trans sid and register globals and i do not use a hoster on which i can put all this on off/0 (with htaccess or a custom php.ini) if ever it's on.

I actually understood the if... in the library and, just to be sure, had a look at my phpinfo once more, but no, safe mode off, that's why i posted my correction here

thanks for answering
have swing
#5

[eluser]kenjis[/eluser]
This seems to be not a CI problem.

How about this PHP code?

Code:
$headers  = "From: ".utf8_decode($from)."\n";
$headers .= 'Reply-To: '.$data['email']."\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$success  = @mail($to, stripslashes(utf8_decode($sujet)), stripslashes(utf8_decode($body)), $headers, '-f ' . $data['email']);

Or the last line:

Code:
$success  = @mail($to, stripslashes(utf8_decode($sujet)), stripslashes(utf8_decode($body)), $headers, '-f' . $data['email']);

If PHP error occures, what the error message?
#6

[eluser]virtualgadjo[/eluser]
well, actually, i used the CI syntax
Code:
$config = array(
    'protocol' => 'mail'
);
$this->load->library('email', $config);
$this->email->from($data['email'], $data['prenom'].' '.$data['nom']);
$this->email->to('[email protected], [email protected]');
$this->email->subject($data['objet']);
$this->email->message($body);

$success = $this->email->send();
if ( $success == TRUE ) $this->load->view('contactSuccess');

and as it sent me this error
Quote:Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
i tried using the raw php syntax (the one you quote) without the -f and everything worked ok (well, to be honest, i've been sending mail this way for more than 10 years... Smile)

that made me think that my server was configured to send mails with the php mail function (once more to be exhautive on the subjet, i had it running very well in raw php before i decided to refactor the whole site in CI because i'm going to develop quite a complex CRM in it, so i knew mail() was working very well...)

i must admit i was about to send the mail without using CI mail class but i don't like to give up things... and changing
Code:
$this->_header_str, "-f ".$this->clean_email($this->_headers['From']))
into this
Code:
$this->_header_str
had it work instantly without changing a comma in the above usage of the class

here are my phpinfos about safe mode, local and master value
Code:
safe_mode               Off        Off
safe_mode_exec_dir      no value    no value
safe_mode_gid           Off         Off
safe_mode_include_dir   no value    no value
as you can see, safe mode is off for both so we can't assume that the master value would overwrite the local one when sending mail on the server

to be complete, php 5.2.17 on an Apache 2.0 Handler/Linux of course

i understand all this may sound a bit strange as i'm sure if CI dev set the mail function when safe mode is off, there must be a very good reason Smile but... it doesn't work for me this way...
now maybe i've forgotten something in my usage of the class, if so, i'd really glad to know

thanks again for taking time about this

have swing
#7

[eluser]virtualgadjo[/eluser]
hi again,

i've tried your syntax to see what happens
Code:
$success  = mail($to, stripslashes(utf8_decode($sujet)), stripslashes(utf8_decode($body)), $headers, '-f' . $data['email']);
(it seems to me that both your last line are the same)

without the @ for php to be able to send me errors, no error but no mail either Smile
it may be that the additional parameters aren't allowed on the server even if not using the safe mode
or, if we refer to the manual (our best friend...)
Quote:The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
because the website MX are not managed by the hoster but but google apps, this could have our users not being added as trusted ones to the send mail config

in this case, i must not be the only one running into this problem as many companies are doing the same with their mails. the optional parameters being... optional Smile particularly when it comes to this envelop thing with -f, i think i'll go without them Smile

Have swing




Theme © iAndrew 2016 - Forum software by © MyBB