简体   繁体   中英

Check SMTP mail send or not

This is my smtp code to send emails:

function smtp_mail($from, $to, $subject, $message, $headers="", $params=array()) {
$errno = "";
$errstr = "";
$smtpConnect = fsockopen("mail2.allramhosting.com", "25", $errno, $errstr, 20);


if (!empty($smtpConnect)) {

    if ($params['use_html']) {
        $extra_headers .= "MIME-Version: 1.0\nContent-Type: text/html; charset=\"utf-8\"\nContent-Transfer-Encoding: 7bit\n";
    }
    $headers = "To: " . $to . "\nFrom: " . $from . "\nSubject: " . $subject . "\nDate: " . date('r') . "\n" . $extra_headers . $headers;

    if (substr($headers, -1) == "\n") $headers = substr($headers, 0, -1); // The last character should not be newline


    fputs($smtpConnect, "HELO networkssms.com\r\n");
    fputs($smtpConnect, "MAIL FROM: " . $from . "\r\n");
    fputs($smtpConnect, "RCPT TO: " . $to . "\r\n");
    fputs($smtpConnect, "DATA\r\n");
    fputs($smtpConnect, $headers . "\r\n\r\n");
    fputs($smtpConnect, $message . "\r\n.\r\n"); // send message and finish
    fputs($smtpConnect,"QUIT" . $newLine);
    fclose($smtpConnect);
    return true;
}
return false;

}

This is the calling code of smtp mail function:

smtp_mail($from, $user_to ,$user_subject, $user_message,"MIME-Version: 1.0\nContent-Type: text/html; charset=\"utf-8\"\nContent-Transfer-Encoding: 7bit\n");

The $from,$user_to ,$user_subject, $user_message have their own values.

I want to check whether the email was sent or not and if it's not sent I want to send it again.

How can I do this ?

First you should check the connection was made ok :

$smtpConnect = fsockopen("mail2.allramhosting.com", "25", $errno, $errstr, 20);

fsockopen returns false if the connection failed. So add the below mentioned code

if (!$smptConnect) {
   echo "ERROR: $errno - $errstr".PHP_EOL;
   return false;
} else {
   // send 
   return true;
}

Each command you issue to the SMTP server will give a numeric response - you should check the response is correct before continuing to the next command - once you read the last comment and all is still OK - this is confirmation the email is sent .. see this link which explains what response you should get from each command

So for example

fputs($smtpConnect, "HELO networkssms.com\r\n");

should respond with code 250 to check this you can do :

fputs($smtpConnect, "HELO networkssms.com\r\n");
$rcode = fgets($smtpConnect, 256);
if (substr($rcode, 0, 3)!= '250') {    // check the first 3 characters ie the response code
   echo "email sending failed" . PHP_EOL;
}

you would need to do this for everyone of your commands - checking the response code is as expected for each command. You could create a simple function to check each response

function check_response($smtpConnect,$expected_response) {
    $rcode = fgets($smtpConnect, 256);
    if (substr($rcode, 0, 3)!= $expected_response) {    // check the first 3 characters ie the response code
       echo "email sending failed" . PHP_EOL;
       return false;
    }
    return true;
}

Then you can do :

fputs($smtpConnect, "HELO networkssms.com\r\n");
if (!check_reponse($smptConnect,'250')) {
   return false;
}

Again you would need to do this for each command sent. I'm sure you get the idea ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM