使用mail()函数无法使用文件发送邮件

时间:2018-04-10 10:46:53

标签: php email

所以这是我的代码,一切看起来都不错,但无法发送电子邮件,也不会显示错误。我认为html代码不是必需的,它是一个带上传字段的简单表单,效果很好。

 <?php

//echo "<pre>"; var_dump($_FILES); exit;

if (isset($_FILES['files'])) {
    $files_array = array();
    $file_count = count($_FILES['files']['name']);
    $file_keys = array_keys($_FILES['files']);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $files_array[$i][$key] = $_FILES['files'][$key][$i];
        }
    }
}

$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('Y.m.d');
$time = date('H:i:s');

$rn = "\r\n";
$to = 'asdds@gmail.com';
$from    = $_POST['email'];
$subject = "Support for: " . $_POST['module'];

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

$body =  "<h3>Support for {$_POST['module']} module</h3>
            <p><strong>Name: </strong> {$_POST['fullname']} </p>
            <p><strong>Email: </strong> {$_POST['email']} </p>
            <p><strong>Site URL: </strong> {$_POST['site_url']} </p>
            <p><strong>Admin URL: </strong> {$_POST['admin_url']} </p>
            <p><strong>Module: </strong> {$_POST['module']} </p>
            <p><strong>Module modified: </strong> {$_POST['module_modified']} </p>
            <p><strong>Admin Username: </strong> {$_POST['admin_username']} </p>
            <p><strong>Admin Password: </strong> {$_POST['admin_password']} </p>
            <p><strong>FTP Host: </strong> {$_POST['ftp_host']} </p>
            <p><strong>FTP Name: </strong> {$_POST['ftp_name']} </p>
            <p><strong>FTP Password: </strong> {$_POST['ftp_password']} </p>
            <p><strong>Message: </strong> {$_POST['message']} </p>
            <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

$headers =  'From: ' . $from  . $rn .
                        'Reply-To: ' . $from  . $rn .
                        'MIME-Version: 1.0' . $rn .
                        'Content-Type: text/html; charset=UTF-8' .  $rn .
                        'Para: WebSite'  .  $rn .
                        'X-Mailer: PHP/' . phpversion();

foreach ($files_array as $file) {
    $content = file_get_contents($file['tmp_name']);
    $content = chunk_split(base64_encode($content));
    $body .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"{$file['name']}\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"{$file['name']}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
    $body .= "--{$mime_boundary}\n";
}

mail($to,$subject,$body,$headers);

当我删除有关附件文件的部分时,它工作正常。如果有人可以帮助我?

1 个答案:

答案 0 :(得分:0)

包装您的邮件功能,以便您可以一睹其失败的原因:

if !( mail($to,$subject,$body,$headers) ) {
  // Any useful feedback?
  print_r(error_get_last());
}

当有更多信息时,我会扩大答案 - 我没有  有足够的声誉评论。