【问题标题】:php mail() function to send mail with an attachmentphp mail() 函数发送带有附件的邮件
【发布时间】:2016-04-14 10:08:12
【问题描述】:

我为这个 HTML 和一个相应的 PHP 创建了两个脚本。

HTML 标记:

<html>
  <body>
    <form action="http://senindiaonline.in/admar/non-series-numbers-db.php" method="post" target="_blank" name="non_series_numbers_db" enctype="multipart/form-data">
      <label for="emp-id">Employee ID: </label>
      <input type="text" name="emp-id" id="emp-id" maxlength="15" style='text-transform:uppercase'/>
      <br/>
      <label for="no-csv">If you have a mobile number database in CSV file format upload that here: </label>
      <input name="no-csv" id="no-csv" type="file" accept=".csv"/>
      <br/>
      <label for="no-xls">If you have a mobile number database in XLS (excel) file format upload that here: </label>
      <input name="no-xls" id="no-xls" type="file" accept=".xls"/>
      <br/>
      <label for="no-xlsx">If you have a mobile number database in XLSX (excel) file format upload that here: </label>
      <input name="no-xlsx" id="no-xlsx" type="file" accept=".xlsx"/>
      <br/>
      <input name="send" value="Send" type="submit" id="send"/>
    </form>
  </body>
</html>

PHP 脚本:

<?php
error_reporting(0);
$email_to = 'senindiagroup@gmail.com';
$email_subject = "Non-Seriesed Mobile Number Database from Ad&Marketing Employee Panel of Sen India Online inc.";    
$emp_id = $_POST['emp-id'];
$sub="Employee ID: $emp_id";
$bound_text = "jimmyPI23";
$bound = "--" .$bound_text. "\r\n";
$bound_last = "--" .$bound_text. "--\r\n";
$email_message .= "If you can see this MIME than your  client doesn't accept MIME types! \r\n" .$bound;
$email_message .= "Content-Type: text/html; Charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n" ."$sub\r\n" .$bound;

//getting temporary file location with name
$a=file_get_contents($_FILES['no-csv']['tmp_name']);
$b=file_get_contents($_FILES['no-xls']['tmp_name']);
$c=file_get_contents($_FILES['no-xlsx']['tmp_name']);

//getting original name of the file you uploaded
$name=$_FILES["no-csv"]["name"];
$name1=$_FILES["no-xls"]["name"];
$name2=$_FILES["no-xlsx"]["name"];

$email_message .= "Content-Type: {\"application/octet-stream\"}; \n" . "name=\"$name\ "\n" . "Content-Transfer-Encoding: base64\r\n" ."Content-Disposition: attachment; \n" . " filename=\"$name\ "\n"."\r\n" .chunk_split(base64_encode($a)).$bound;

mail($email_to, $email_subject, $email_message, $headers);
?>

错误是:

第 31 行显示语法错误,使用邮件命令附加文件。

并且邮件没有发送。我用 DreamWeaver 的建议解决了这个错误。然后发送邮件,但邮件中不包含附件。

【问题讨论】:

  • 我刚刚用谷歌搜索并得到了这个链接:stackoverflow.com/questions/12301358/…
  • 我用正确的 HTML 元素标签 'label' 替换了所有的 'lebel' 标签。
  • 说真的...停止尝试使用mail() 发送附件。可以做到,但您需要编写大量代码。相反,只需下载 phpMailer 或 Swiftmailer,并使用其中之一。他们已经为您完成了所有工作,因此您只需将他们包括在内并给他们打电话。它们还解决了发送电子邮件的许多安全问题,并且比简单的mail() 功能具有额外的功能,例如能够通过外部 smtp 服务器发送。

标签: php html email


【解决方案1】:

这是在邮件中发送多个附件的示例

<?php

    for($i=0; $i < count($_FILES['csv_file']['name']); $i++){

        $ftype[]       = $_FILES['csv_file']['type'][$i];
        $fname[]       = $_FILES['csv_file']['name'][$i];

    }


    // array with filenames to be sent as attachment
    $files = $fname;

    // email fields: to, from, subject, and so on
    $to = "example@gmail.com";
    $from = "example@gmail.com"; 
    $subject ="My subject"; 
    $message = "My message";
    $headers = "From: $from";

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

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

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

    // preparing attachments
    for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // send

    $ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 


?>

如果您想要简单的解决方案,请使用 phpmailer 库,这将易于使用,易于配置。

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 2011-06-05
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2020-01-27
    • 2016-05-03
    相关资源
    最近更新 更多