返回false的PHP脚本不确定原因

时间:2016-07-26 00:39:27

标签: php

我的脚本无效,我不知道为什么。有什么建议?

<?php
    class email
    {
        function emailPlusAttachment($fromAddress,$toAddress,$mailSubject,$mailMessageHead,$mailMessageMain,$mailMessageSign,$filePath,$fileName)
        {

            $fileatt_name = $fileName;
            $fileatt = $filePath.$fileName;
            $fileatt_type = "application/octet-stream";
            $email_from = $fromAddress;
            $email_subject = $mailSubject;

            $email_message = $mailMessageHead. "<br>";
            $email_message .= $mailMessageMain. "<br>";
            $email_message .= $mailMessageSign;

            $email_to = $toAddress;

            $headers = "From: ".$email_from;

            $file = fopen($fileatt, 'rb');
            $data = fread($file, filesize($fileatt));
            fclose($file);

            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

            $headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed:\n" .
            " boundary=\"{$mime_boundary}\"";

            $email_message .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $email_message .= "\n\n";

            $data = chunk_split(base64_encode($data));

            $email_message .= "--{$mime_boundary}\n" .
            "Content-Type: {$fileatt_type}; \n" .
            " name=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data .= "\n\n" .
            "--{$mime_boundary}--\n";

            if (@mail($email_to, $email_subject, $email_message, $headers)) 
            {
              return true;
            }
        }
    }
?>

使用:     

    $from       =   'sender@tjaa.co.uk';
    $sendto     =   'recipient@hotmail.co.uk';
    $subject    =   'email with one attachment';
    $bodyHead   =   'Hello Drew';
    $bodyMain   =   'There is an attachment associated with this email.';
    $bodyEnd    =   'Thank you';
    $filePath   =   '';
    $fileName   =   'logo.png';

    if ($sendEmail->emailPlusAttachment($from,$sendto,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
    {

        echo "email sent";
    }   
    else {echo "email not sent";}

?>

只是echo s“电子邮件不发送”,表示返回false ...任何指针?

语法错误的可能性有多大,或者有人能识别出更大的问题?

2 个答案:

答案 0 :(得分:1)

"Content-Type: multipart/mixed:\n" .

尝试将第二个:更改为;

我使用Mailcatcher来测试您的脚本,这就是beforeafter标题修复的样子。请注意,邮件选项卡位于HTML上,而不是源。

Mailcatcher非常适合测试电子邮件,但如果您不想进行设置,Mailinator也非常好(如果您的ISP不阻止所有邮件发送端口)。< / p>

答案 1 :(得分:1)

要考虑的另一件事(除了杏子男孩关于标题的评论)是你的sendmail / mail()程序是否正确配置为PHP发送 - 即以正确的权限运行。一般来说,mail()可以在写入sendmail管道后无声地失败或关闭,而无法访问错误,如果配置不正确,sendmail可能会丢弃电子邮件。

如果使用sendmail,PHP.ini是否也设置了sendmail程序,而在sendmail配置中,apache用户是否是可信用户文件的成员?

运行一个简单的&#34; hello world&#34; mail()测试给自己,它经历了吗?我认为有两件事需要测试和固定。如果杏子的变化无法修复,则需要更多信息进行诊断。