ホームページ  >  記事  >  バックエンド開発  >  Linuxシステム上のPHPでメールを送信できない

Linuxシステム上のPHPでメールを送信できない

WBOY
WBOYオリジナル
2016-06-23 14:03:05998ブラウズ

Linux システムで次のコードを使用して電子メールを送信したところ、送信が成功したことを示すメッセージが表示されましたが、実際には電子メールはまったく送信されませんでした。その理由は何なのか、ぜひご覧ください。

Mail.inc.php ファイルコード:
mail.inc.php の開始-------------------------------- - -------------------
& lt;? PHP
// メール
クラス smtp {
/*パブリック変数変数*/
var $ smtp_port; ; $ホスト名;
var $debug;
var $test; /* プライベート変数 */
var $sock;

/* Contractor */

function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass,$from)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->>time_out = 30;
$this-> = $from; $this->sock = FALSE;
/* メイン関数 */

function sendmail($to,$fromname="",$subject = "" 、$body = ""、$mailtype、$cc = ""、$bcc = ""、$Additional_headers = "")
$mail_from $mail_from ", $body);
$header = "MIME バージョン:1.0rn ";

if($mailtype=="HTML"){
$header .= "Content-Type:text/htmlrn";
}

$header .= "To: ".$to."rn"; ️
if ($fromname!="") $header .= "From: ".$fromname."<".$this->from.">rn"; $header .= "From:< ;".$this->from.">rn";
$header .= "件名: ".$subject."rn";
$header .= $Additional_headers;
        $header .= "日付: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
$utfheader=iconv("GBK","UTF-8//IGNORE",$header);
list($msec, $sec) = explode(" ", microtime());

$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";

$TO = explode(",", $this->strip_comment($to));

if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}


if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}

$sent = TRUE;

foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);

if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("エラー: ".$rcpt_to."n" にメールを送信できません);
$sent = FALSE;
続けます。
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $utfheader, $body)) {
$this- >log_write("電子メールが送信されました<".$rcpt_to.">n");
} else {
$this->log_write("エラー: <".$rcpt_to.">n"); にメールを送信できません。
$sent = FALSE;
}

fclose($this->sock);

$this->log_write("リモートホストnから切断されました");
}
$sent を返す;
}
/* プライベート 関数 */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO "、$helo )) {

return $this->smtp_error("sending HELO command");
}

#auth

if($this->auth){
            if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error (「HELO コマンドの送信」);
}
}

#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("コマンドからメールを送信します」); if(!$ this-&gt; smtp_putcmd( "rcpt"、 "to:&lt;"。$ to。 "&gt;")){
$ $ this-&gt; smtp_error( "rcptをコマンドに送信する" );
}

if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("送信中 . [EOM]");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
}
TRUEを返します。
}}関数smtp_sockopen($ address)
{
if($ this-&gt; reay_host == ""){
$ $ this-&gt; smtp_sockopen_mx($ address);
} else {
return $this->smtp_sockopen_relay();
}}} function smtp_sockopen_relay()
{
$ this-&gt; log_write( "Trise"。$ this-&gt; reay_host。 ":": "。$ this-&gt; smtp_port。" n ");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok​​())) {
$this->log_write("エラー: リレーホストに接続できません。".$this->relay_host."n ");
            $this->log_write("エラー: ".$errstr." (".$errno.")n");
FALSE を返します。
}
$this->log_write("リレーホストに接続されました".$this->relay_host."n");
TRUE を返します。
}

function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "1", $address);
if (!@getmxrr($domain, $MXHOSTS)) {
$this->log_write("エラー: MX を解決できません"".$domain.""n");
FALSE を返します。
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok​​())) {
$this->log_write("警告: mx ホストに接続できません".$host."n");
$this->log_write("エラー: ".$errstr." (".$errno.")n");
続けます。
}
$this->log_write("MX ホストに接続されました".$host."n");
TRUE を返します。
}
$this->log_write("エラー: どの mx ホストにも接続できません (".implode(", ", $MXHOSTS).")n");
FALSE を返します。
}

function smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n".">gt; ", $header."n>gt; ".$body."n> "));
TRUE を返します。
}

function smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok​​();
}

function smtp_ok​​()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
            fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("エラー: リモートホストが返されました"".$response.""n");
FALSE を返します。
}
TRUEを返します。
}

function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
return $this->smtp_ok​​();
}

function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".n");
FALSE を返します。
}

function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("警告: できませんログ ファイルを開きます。"".$this->log_file.""n");
FALSE を返す;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
TRUE を返します。
}

function strip_comment($address)
{
$comment = "([^()]*)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
$address を返す;
}

function get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "1", $address);
$address を返す;
}
関数 smtp_debug($message)
{
if ($ this-& gt; debug) {
echo $ は言った -----------------------------


これはmail.php ファイルのコード:
-------- ---------------------------------- -------------
< ?php
include_once("mail.inc.php");
$smtp = new smtp("smtp.qq.com",25,true," tangfengme@163.com","781203tf","593894955@qq.com");//送信者のメールボックス情報
$smtp->debug = false;//送信されたデバッグ情報を表示するか FALSE か TRUE
$mailto ="53894955@qq.com";//受信者のメールボックス
$mailsubject="web メッセージ";
$mailbody="Name=".$_POST["q1"]; br>";
$mailbody=$mailbody."電話番号=".$_POST["q2"]."
";
$mailbody=$mailbody."郊外=".$_POST["q3" ]."
";
$mailbody= $mailbody."email=".$_POST["email"]."
";
$mailbody=$mailbody."; ["q4"]."
";
//他のフォーム項目も同様
$mailtype = "HTML" //メール形式(HTML/TXT)、TXTはテキストメールです
$mailsubject = ' =?UTF-8?B?'.base64_encode($mailsubject).'?=';//メール件名
$mailfrom = '=?UTF-8?B?'.base64_encode($mailfrom).'?=' ;//送信者
$smtp->sendmail ($mailto, $mailfrom, $mailsubject, $mailbody, $mailtype)
echo ""; exit();
?>

--------------------------------- ---------- ---------






ディスカッション(解決策)への返信

スパムボックスを確認しますか?

各ステップで考えられる問題をマークし、それをファイルに書き込みます。同時に、対象のメールボックスでスパムを検索します。それが機能しない場合は、ホワイトリストを設定します。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。