我有以下 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>

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');
}
}
?>

编辑邮件成功发送所有的细节.只是附件不会发送.

编辑2:解决更改$mail-> MsgHTML到$mail->正文它工作!

更改
$mail->MsgHTML();

$mail->Body;

资料来源:here

通过PHPMailer添加附件的更多相关文章

  1. ios – 在Restkit 0.2中为给定的类添加两个请求描述符

    我需要从User类中提供两种不同类型的POST.我试图制作两个请求描述符并将其添加到我的对象管理器,但是我收到错误“Cannotaddarequestdescriptorforthesameobjectclassasanexistingrequestdescriptor.”我的代码有没有人知道如何解决这个问题,而不需要为这些请求创建一个单独的类?任何帮助是赞赏.谢谢.解决方法您可以使用动态映射来切

  2. [快速学会Swift第三方库] SQLite.swift篇

    [快速学会Swift第三方库]sqlite.swift篇sqlite.swift是一个使用纯Swift语言封装sqlite3的操作框架。

  3. CSSwiftExtension - 贡献一个非常好用的Swift extension集合

    CSSwiftExtension是我个人开源的一个非常有用的Swiftextension集合。

  4. Swift类型不符合协议NilLiteralConvertible

    我以为写这样的东西:这不会抛出这个错误。听起来像斯威夫特让我只写第二种方式。如果你的函数可以返回nil,那么它应该被声明为…然后呼叫者知道它可以是零。(你的第二个例子是工作,但会崩溃,因为退出将隐含地尝试解开nil。

  5. swift3 – Alamofire 4.0,Swift 3 Post params没有通过

    当我升级到最新的一切时,以下停止发布参数,我不知道为什么……

  6. 无法使用swift 3和ios 10从facebook检索电子邮件

    您好我正试图从Facebook检索我的电子邮件,因为我正在使用swift播放facebookiossdk.IOS平台是10,swift3和Xcode8.我在线跟踪教程,但无法检索电子邮件.下面是我的代码:在我的appdelegate.swift文件中:我能够登录并注销但无法检索电子邮件.更新实际上,当我实际传递打印时,我可以在控制台上看到它作为可选语句.我在没有可选参数的情况下显示它时遇到了麻烦我用这种方式解决了这个问题:

  7. android – 如何在ViewPager中使用cursorLoader?

    解决方法我无法评论,所以我正在写一个答案..您有一个实现LoaderCallbacks的活动.加载数据时,您的活动会收到onLoadFinished回调.在此方法中,您有一个应该在ViewPager中显示的Cursor.要显示Cursor中的数据,请在适配器上调用swapCursor方法.因此,每次加载数据时都不要创建适配器.创建一次,然后只需调用swapCursor即可.此外,每次都找不到ViewPager–findViewById是一个繁重的操作,它应该在创建视图层次结构后执行.所以,你的onLoad

  8. 使用LoginManager登录Android Facebook SDK 4.0

    我正在尝试将旧应用程序的登录代码从SDK3.0迁移到SDK4.0.我已经使用LoginManager实现了Login,因为我有自定义登录按钮.问题是我没有得到FacebookAPI的回复.没有成功,没有错误,没有任何异常抛出.代码如下:使用调试器,我能够看到这一行:mLoginMgr.logInWithReadPermissions(mActivity,mPermissions);执行,但没有触发

  9. android – UnitTest JSONObject显示为null

    )将其添加到build.gradle文件中,如下所示这将取代存根的Android库与在桌面上工作的库.

  10. 如何在android中解析这个JSON数组

    我想要每个TAG的名称,电子邮件和图像.我必须在List元素中显示.我尝试了很多但却无法做到.只是让我知道循环,如何达到每个名称,电子邮件,图像等的值,如何保持.解决方法我为你的问题生成了一个解决方案.

随机推荐

  1. PHP个人网站架设连环讲(一)

    先下一个OmnihttpdProffesinalV2.06,装上就有PHP4beta3可以用了。PHP4给我们带来一个简单的方法,就是使用SESSION(会话)级变量。但是如果不是PHP4又该怎么办?我们可以假设某人在15分钟以内对你的网页的请求都不属于一个新的人次,这样你可以做个计数的过程存在INC里,在每一个页面引用,访客第一次进入时将访问时间送到cookie里。以后每个页面被访问时都检查cookie上次访问时间值。

  2. PHP函数学习之PHP函数点评

    PHP函数使用说明,应用举例,精简点评,希望对您学习php有所帮助

  3. ecshop2.7.3 在php5.4下的各种错误问题处理

    将方法内的函数,分拆为2个部分。这个和gd库没有一点关系,是ecshop程序的问题。会出现这种问题,不外乎就是当前会员的session或者程序对cookie的处理存在漏洞。进过本地测试,includes\modules\integrates\ecshop.php这个整合自身会员的类中没有重写integrate.php中的check_cookie()方法导致,验证cookie时返回的username为空,丢失了登录状态,在ecshop.php中重写了此方法就可以了。把他加到ecshop.php的最后面去就可

  4. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  5. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  6. zen_cart实现支付前生成订单的方法

    这篇文章主要介绍了zen_cart实现支付前生成订单的方法,结合实例形式详细分析了zen_cart支付前生成订单的具体步骤与相关实现技巧,需要的朋友可以参考下

  7. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 基于win2003虚拟机中apache服务器的访问

    下面小编就为大家带来一篇基于win2003虚拟机中apache服务器的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. Yii2中组件的注册与创建方法

    这篇文章主要介绍了Yii2之组件的注册与创建的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

返回
顶部