Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHPMailer添加附件_Php_Email_Phpmailer - Fatal编程技术网

通过PHPMailer添加附件

通过PHPMailer添加附件,php,email,phpmailer,Php,Email,Phpmailer,我有下面这段PHPMailer代码。问题是,文件已成功上载到服务器,但附件未通过邮件发送。据我所知,附件代码似乎是正确的。请检查代码并让我知道我哪里出错了 表格 <form name="contactform" method="post" action="send1.php" enctype="multipart/form-data"> <table width="100%" border="0"> <tr> <td id="ta"> <

我有下面这段PHPMailer代码。问题是,文件已成功上载到服务器,但附件未通过邮件发送。据我所知,附件代码似乎是正确的。请检查代码并让我知道我哪里出错了

表格

<form name="contactform" method="post" action="send1.php" enctype="multipart/form-data">
<table width="100%" border="0">
<tr>
 <td id="ta">
 <label for="title">Title *</label>
 </td>
 <td id="ta">
 <select name="title">
 <option value="0">Title</option>
 <option value="1">Mr.</option>
 <option value="2">Ms.</option>
 <option value="3">Mrs.</option>
 </select></td></tr><tr><td id="ta">
  <label for="first_name">First Name *</label>
 </td>
 <td id="ta">
  <input  type="text" name="first_name" maxlength="50" size="30" required="required">
 </td>
</tr>
<tr>
 <td id="ta">
  <label for="last_name">Last Name *</label>
 </td>
 <td  id="ta">
  <input  type="text" name="last_name" maxlength="50" size="30" required="required">
 </td>
</tr>
<tr>
 <td id="ta">
  <label for="email">Email Address *</label>
 </td>
 <td  id="ta">
  <input  type="text" name="email" maxlength="80" size="30" required="required">
 </td>
</tr>
<tr>
 <td id="ta">
  <label for="telephone">Telephone Number *</label>
 </td>
 <td  id="ta">
  <input  type="text" name="telephone" maxlength="30" size="30" required="required">
 </td>
</tr>
<tr>
 <td id="ta">
  <label for="comments">Details</label>
 </td>
 <td  id="ta">
  <textarea  name="comments" maxlength="100000" cols="25" rows="6"></textarea>
 </td>
</tr>
<tr>
<td id="ta">
    <label for="file">Or upload a file (only word, excel or pdf)</label>
</td>
<td  id="ta">
<input type="file" name="file">
</td>
</tr>
<tr>
 <td colspan="2" style="text-align:center" id="ta">
  <input type="submit" value="Submit">
 </td>
</tr>
</table>
</form>

头衔*
标题
先生
太太
夫人
名字*
姓*
电子邮件地址*
电话号码*
细节
或上传文件(仅限word、excel或pdf)
send1.php

<?php

require('PHPMailer/class.phpmailer.php');

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    //$email_to = "hidden";
    //$email_subject = "Request for Portfolio check up from ".$first_name." ".$last_name;

    $title = array('Title', 'Mr.', 'Ms.', 'Mrs.');
    $selected_key = $_POST['title'];
    $selected_val = $title[$_POST['title']]; 

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

  if(($selected_key==0))
    echo "<script> alert('Please enter your title')</script>";
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     $email_message = "";
    $email_message .="Title: ".$selected_val."\n";
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

    $allowedExts = array("doc", "docx", "xls", "xlsx", "pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/excel")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
|| ($_FILES["file"]["type"] == "application/x-excel")
|| ($_FILES["file"]["type"] == "application/x-msexcel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))

&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>";
    }
  else
    {
        $d='upload/';
        $de=$d . basename($_FILES['file']['name']);
    move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$fileName = $_FILES['file']['name'];
    $filePath = $_FILES['file']['tmp_name'];
     //add only if the file is an upload
     }
  }
else
  {
  echo "<script>alert('Invalid file')</script>";
  }

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug  = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host       = "hidden";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port       = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth   = true;
//Username to use for SMTP authentication
$mail->Username   = "hidden";
//Password to use for SMTP authentication
$mail->Password   = "hidden";
//Set who the message is to be sent from
$mail->SetFrom($email_from, $first_name.' '.$last_name);
//Set an alternative reply-to address
//$mail->AddReplyTo('replyto@example.com','First Last');
//Set who the message is to be sent to
$mail->AddAddress('hidden', 'hidden');
//Set the subject line
$mail->Subject = 'Request for Profile Check up';
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
$mail->MsgHTML($email_message);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->AddAttachment($file);
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
//Send the message, check for errors
if(!$mail->Send()) {
  echo "<script>alert('Mailer Error: " . $mail->ErrorInfo."')</script>";
} else {
  echo "<script>alert('Your request has been submitted. We will contact you soon.')</script>";
  Header('Location: main.php');
}
}
?>
0)
{
回显“警报('Error:'.$\u FILES[“file”][“Error”].“)”);
}
其他的
{
$d='upload/';
$de=$d.basename($_FILES['file']['name']);
移动上传的文件($文件[“文件”][“tmp文件名”],$de);
$fileName=$_文件['file']['name'];
$filePath=$\u文件['file']['tmp\u名称'];
//仅当文件是上载文件时添加
}
}
其他的
{
回显“警报('无效文件')”;
}
//创建电子邮件标题
$headers='From:'.$email\u From.\r\n。
'回复:'.$email\u from.\r\n'。
“X-Mailer:PHP/”。phpversion();
//创建新的PHPMailer实例
$mail=new PHPMailer();
//告诉PHPMailer使用SMTP
$mail->IsSMTP();
//启用SMTP调试
//0=关闭(用于生产使用)
//1=客户端消息
//2=客户端和服务器消息
$mail->SMTPDebug=0;
//请求HTML友好的调试输出
$mail->Debugoutput='html';
//设置邮件服务器的主机名
$mail->Host=“hidden”;
//设置SMTP端口号-可能是25、465或587
$mail->Port=25;
//是否使用SMTP身份验证
$mail->SMTPAuth=true;
//用于SMTP身份验证的用户名
$mail->Username=“hidden”;
//用于SMTP身份验证的密码
$mail->Password=“hidden”;
//设置要从谁发送消息
$mail->SetFrom($email\u from,$first\u name.'.$last\u name);
//将替代答复设置为地址
//$mail->AddReplyTo('replyto@example.com","最后一次",;
//设置要向谁发送邮件
$mail->AddAddress('hidden','hidden');
//设置主题行
$mail->Subject='档案检查请求';
//从外部文件读取HTML消息正文,将引用的图像转换为嵌入的,将HTML转换为基本的纯文本替代正文
$mail->MsgHTML($email\u message);
//将纯文本正文替换为手动创建的正文
$mail->AltBody='这是一个纯文本消息正文';
//附加图像文件
//$mail->AddAttachment($file);
$mail->AddAttachment($\u FILES['file']['tmp\u name'],$\u FILES['file']['name']);
//发送消息,检查错误
如果(!$mail->Send()){
回显“警报('Mailer Error:“.$mail->ErrorInfo.”);
}否则{
echo“警报('您的请求已提交。我们将很快与您联系')”;
标题('Location:main.php');
}
}
?>
编辑邮件发送成功,并包含所有详细信息。只是附件不会发送

编辑2:已解决
$mail->MsgHTML
更改为
$mail->Body
,并且成功了

变化

$mail->MsgHTML();


来源:

是的,文件上传正确,我检查了它的解决方法:将
$mail->MsgHTML()
更改为
$mail->Body
,它可以工作!所以禁止我回答/提问,所以请投票给我!:派斯。。。非常感谢。所以不允许我再回答了:(出于某种原因,我不明白,这真的起作用了。PhpMailer在添加主体时创建的附加头肯定有问题
$mail->Body;