Search code examples
phpssmtp

unable to send mail via php and ssmtp


There are no errors in my code from what I can find and my local ssmpt client is configured and I can send mail via the command line. But no mail and no error come from php at all. It's as though php never hits this local mailer. I 'do' have my sendmail path set in my php.ini and restarted apache. I don't know what else it could be?

<?php
require_once('config.php');
$attrs = array(PDO::ATTR_PERSISTENT => true);
$pdo = new PDO("mysql:host=localhost;dbname=".$dbname, $db_username, $db_password, $attrs);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$custlist = $pdo->prepare("SELECT customer_list.sms_num, carriers.carrieraddr, customer_list.contact_first,  customer_list.contact_last
    FROM carriers, customer_list
    WHERE send_id=send_code
    ORDER BY customer_list.sms_num");
#$carrierlist = $pdo->prepare("SELECT * FROM carriers");
if (isset($_POST['body'], $custlist)) {
    $custlist->execute();
    #var_dump($custlist);
    #var_dump($_POST['body']);
    while ($row = $custlist->fetch(PDO::FETCH_ASSOC)) {
        #$prefix = $row['sms_num'];
        #$suffix = $row['carrieraddr'];
        $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);
        #$body = $_POST['body'];

        #var_dump($prefix);
        #var_dump($suffix);
        #var_dump($_POST['body']);
    }
}

//$products = array();
$smscustobject = new ArrayObject($custlist);
#$smsarrayobject = new ArrayObject($carrierlist);

$pdo = null;
?>
<form action="" method="POST">
    Your Message Body <input name="body" type="text" />
    <input type="submit">
</form>

Solution

  • I had the same problem and found after running a port testing PHP script that I needed to configure linux (Centos in my case) to allow httpd to access TCP ports using the following command on the command line:

    setsebool httpd_can_network_connect=1

    or permanently by

    setsebool -P httpd_can_network_connect=1

    NB: using the -P option took over 2 minutes on my machine so be patient

    Hey presto! my mail command worked.

    My situation was that I was able to send mail to my ISP's SMTP Server ie mail.optusnet.com.au Port 25 from the command line but not using PHP scripts ran in my browser.

    Also, ensure that your firewall is not interfering with PHP by running a Telnet session to you SMTP host.

    eg $telnet mail.optusnet.com.au 25

    In your case I suspect not as you are able to send emails from the Command Line.

    I hope that helps