从php表单生成的空白电子邮件

时间:2014-03-01 18:54:49

标签: php email-attachments

我有以下代码,当有人点击表单上的发送按钮时执行。

我的问题是我收到的电子邮件中包含正确的主题和电子邮件地址。但是,如果电子邮件包含附件,则正文为空白。但是,如果他们没有电子邮件附件,则电子邮件正文将填充用户输入的文本。

我应该注意到我理解代码不像在当前状态下那样安全,不会限制像exe这样的不适当文件被上传。但是,我不想要或者需要有关如何使代码安全的教训。我只是需要帮助解决上传附件时文本正文不会填充的原因/原因?

     <?php
         //send email
          if(isset($_POST['sendemail']))
            {
              $txtSid = md5(uniqid(time()));
              $emailsubject = $_POST['emailsubject'];
              $emailpriority = $_POST['emailpriority'];
              $emailto = $_POST['emailto'];
              $emailcc = $_POST['cc'];
              $emailbcc = $_POST['bcc'];
              $txtFilesName = $_FILES["attachment"]["name"];
              $emailbody = $_POST['wysiwg_editor'];

              //if their is an attachment add notice in body of email
               if($txtFilesName !='')
                  {
                    $emailbody = "$emailbody <br /><br />File name $txtFilesName attached to this email.";
                  }                                           

$headers = "From: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "Reply-To: $my_fname $my_lname <$my_emailaddr>\r\n";
$headers .= "CC: $emailcc\r\n";
$headers .= "BCC: $emailbcc\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$txtSid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= $emailbody."\n\n";

if($emailpriority == 'high priority')
 {
   $headers .= "X-Priority: 1 (Highest)\n";
   $headers .= "X-MSMail-Priority: High\n";
   $headers .= "Importance: High\n";
 }


   $headers .= "--".$txtSid."\n";
   $headers .= "Content-type: text/html; charset=utf-8\n";
   $headers .= "Content-Transfer-Encoding: 7bit\n\n";


/***********Email Attachment************/
if($_FILES["attachment"]["name"] != "")
{
    $txtFilesName = $_FILES["attachment"]["name"];
    $txtContent = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"]))); 
    $headers .= "--".$txtSid."\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$txtFilesName."\"\n"; 
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$txtFilesName."\"\n\n";
    $headers .= $txtContent."\n\n";
}


mail("$emailto","$emailsubject","$emailbody","$headers");


echo "<div class='alert alert-success'>Email Sent!</div>";
}

好的,我重新复制并粘贴了我正在使用的代码,它解决了我的问题。好像我需要一个$ headers。= $ emailbody。“\ n \ n”; if电子邮件附件部分内的代码行。

1 个答案:

答案 0 :(得分:0)

在检测上传附件的if语句下,您没有将先前的电子邮件主体和通知组合在一起。相反,请使用以下代码:

$emailbody = $emailbody."<br /><br />File name ".$txtFilesName." attached to this email.";