tags:

views:

872

answers:

4

Hello all,

Does anybody know if you can configure php's mail() command so it will only use an SMTP server rather than the local sendmail? We are having trouble with emails being marked as spam.

Our server is running RedHat 5 Enterprise.

I am aware of various PHP libraries that act as an SMTP client but I'd rather configure PHP so mail() used an SMTP server directly.

Thanks for your help.

A: 

According to this manual page, it is possible on Windows only.

Pekka
A: 

Check these links:

Example:

Update: You can use this then, but it opens and closes the SMTP socket on each mail() function called.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Colour Blend
He is asking about native mail() ways to do it.
Pekka
Install the PEAR Mail package.
Colour Blend
Yes, to confirm, I need to get mail() to do it otherwise we've got a whole load of PHP files to rewrite!
Kevin Sedgley
I have provided an update. You can use the previous example if you can include the external library in you index page, which will cascade down to every other php script in your application. all you will have to do then is to change "mail" to "$smtp->send". Also take note of the function arguments.
Colour Blend
+1  A: 

No, the reason is that all Linux/Unix systems should have a "sendmail" tool. The benefit there is that this external tool can handle timeouts or unresponsive SMTP servers so it becomes more likely the mail is being really sent. The SMTP client implementation for Windows is a work-around for the fact that "sendmail" doesn't exist there.

My approach would be to use a sendmail-compatible tool that just talks to another server using SMTP. A simple tool for that is ssmtp (sources avialable here)

johannes
+1  A: 

Just configure your local sendmail to use your upstream mail server as a relay! That way, you don't have to change anything on the PHP side.

It would not be a good idea to send mail directly from PHP with SMTP, because you would lose everything from error handling to queueing this way!

hop