如何修复sendmail.php文件';不行?

如何修复sendmail.php文件';不行?,php,html,sendmail,Php,Html,Sendmail,我有网站,我不专业,但我已经完成除了发送邮件的问题。 当我点击“立即发送”按钮时,屏幕上的信息显示为“感谢您的信息”,但我没有收到任何电子邮件 我选中了垃圾邮件框,它也不在那里 我检查了phpinfo SMTP端口是否打开和25 我检查了phpinfo senmail_path/usr/sbin/sendmail-t-I 我打电话给托管公司并发送电子邮件,他们说一切正常,服务器端检查您的sendmail脚本 这里是sendmail.php文件 <?php $name = @

我有网站,我不专业,但我已经完成除了发送邮件的问题。 当我点击“立即发送”按钮时,屏幕上的信息显示为“感谢您的信息”,但我没有收到任何电子邮件

  • 我选中了垃圾邮件框,它也不在那里

  • 我检查了phpinfo SMTP端口是否打开和25

  • 我检查了phpinfo senmail_path/usr/sbin/sendmail-t-I

  • 我打电话给托管公司并发送电子邮件,他们说一切正常,服务器端检查您的sendmail脚本

这里是sendmail.php文件

<?php
$name       = @trim(stripslashes($_POST['name']));
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'tradertarik@gmail.com';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);
die;

您的
$headers
参数不正确;它应该是
字符串
而不是
数组
。从:

要插入到电子邮件标题末尾的字符串

这通常用于添加额外的标头(从、抄送和密件抄送)。应使用CRLF(\r\n)分隔多个额外的标头。如果使用外部数据组成此标头,则应对数据进行清理,以便不会注入不需要的标头

您应该能够这样解决问题:

mail($to, $subject, $message, implode("\r\n", $headers));

如果sendmail没有正确配置,那么php中的Mail函数很难使用。相反,我使用了(不要惊慌,您只需要其中的2个文件class.phpmailer.php和class.smtp.php)

基于PHPMailer的发送邮件功能示例

function send_mail($subject, $body, $altbody, $to, $name, $attach = '')
{
$mail = new PHPMailer(true);
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = 'text/html; charset=UTF-8;';

$mail->Host       = "some.external.smtp.server";    // SMTP server example
$mail->SMTPDebug  = 0;                      // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                   // enable SMTP authentication
$mail->Port       = port_for_external_smtp;                     // set the SMTP port for the GMAIL server
$mail->Username   = "username_for_external_smtp_server";        // SMTP account username example
$mail->Password   = "pass";             // SMTP account password example

try 
    { 
        $mail->setFrom('address', 'name'); 
        $mail->addReplyTo($to, $name); 
        $mail->addAddress($to, $name);
        $mail->Subject = $subject; 
        $mail->Body = $body;
        $mail->isHTML(true);
        $mail->AltBody = $altbody;  // altbody if for text only mail
        if (!($attach == ''))
        {
                $mail->AddAttachment($attach); // attachment 
            }
        $mail->send(); 
        } 
catch (phpmailerException $e) 
    { 
        echo $e->errorMessage(); //Pretty error messages from PHPMailer 
        } 
catch (Exception $e) 
    { 
        echo $e->getMessage(); //Boring error messages from anything else! 
        }   
}

日志是怎么写的?
mail($to,$subject,$message,$headers)
把这个放在
if(){}
里面,检查它是否返回
FALSE
。呃,检查
mail()的结果如何
var_dump(mail..)
如果它报告您没有发送文件,则问题可能出在您的服务器配置中。邮件($to,$subject,$message,$headers)可能重复,我已更改为timclutton建议的邮件($to,$subject,$message,内爆(\r\n“,$headers));我收到电子邮件,但没有发件人姓名、邮件或主题。现在可能是什么?Hi timcluttonI刚刚收到电子邮件,但没有主题消息和发件人姓名。空电子邮件。可能有什么问题?在调用
mail
之前,您需要通过检查变量的内容来自己调试它。使用类似于
var\u dump($to,$subject,$message,introde(\r\n“,$headers))
查看所有变量的内容。Alex谢谢你的回答,但我已经吓坏了)我不是程序员,在这之后我很可能再也不需要听说php了。只是我需要找到一个简单的方法来解决它。
function send_mail($subject, $body, $altbody, $to, $name, $attach = '')
{
$mail = new PHPMailer(true);
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->CharSet = 'text/html; charset=UTF-8;';

$mail->Host       = "some.external.smtp.server";    // SMTP server example
$mail->SMTPDebug  = 0;                      // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                   // enable SMTP authentication
$mail->Port       = port_for_external_smtp;                     // set the SMTP port for the GMAIL server
$mail->Username   = "username_for_external_smtp_server";        // SMTP account username example
$mail->Password   = "pass";             // SMTP account password example

try 
    { 
        $mail->setFrom('address', 'name'); 
        $mail->addReplyTo($to, $name); 
        $mail->addAddress($to, $name);
        $mail->Subject = $subject; 
        $mail->Body = $body;
        $mail->isHTML(true);
        $mail->AltBody = $altbody;  // altbody if for text only mail
        if (!($attach == ''))
        {
                $mail->AddAttachment($attach); // attachment 
            }
        $mail->send(); 
        } 
catch (phpmailerException $e) 
    { 
        echo $e->errorMessage(); //Pretty error messages from PHPMailer 
        } 
catch (Exception $e) 
    { 
        echo $e->getMessage(); //Boring error messages from anything else! 
        }   
}