PHPMailer的文件附件 [英] File attachment with PHPMailer

查看:69
本文介绍了PHPMailer的文件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单,可以选择上传文件.
然后,我希望将该文件作为附件发送到电子邮件地址,以及其余的表单数据.
我正在使用PHP Mailer,并获取要发送的表单数据:例如姓名,电话号码等.

I have an HTML form with the option to upload a file.
I would like then to send that file as an attachment to the email address along with the rest of the form data.
I'm using PHP Mailer and I get the form data to send: such as name, telephone number, etc.

我无法将图像与图像一起发送.我提供了到目前为止的代码

I can't get the image to send along with it. I've provided the code I have so far

<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>

<body>

<form id='contactus' action='contact.php' enctype="multipart/form-data" method='post'>

<fieldset >
<legend>Contact us</legend>


<div class='container'>
  
    <label for='email' >Name*:</label><br/>
    <input type="text" id="name" name="name" required /><br>
    <label for='email' >Phone*:</label><br/>
    <input type="text" id="phone" name="phone" required /><br>
    <label for='email' >Email*:</label><br/>
    <input type='text' name='email' id='email' required/><br/>
 
    <label for='message' >Message:</label><br/>
    <textarea rows="10" cols="50" name='message' id='message'></textarea>
    <br>
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input id="file" name="image" type="file" />
    <input type='submit' name='Submit' value='Submit' />
</div>

</fieldset>
</form>
    </body>
</html>

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $expensions= array("jpeg","jpg","png","pdf");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a PDF, JPEG or PNG file.";
      }
      
      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true) {
         move_uploaded_file($file_tmp,"uploads/".$file_name); //The folder where you would like your file to be saved
         echo "Success";
      }else{
         print_r($errors);
      }
   }

// PHPMailer script below

$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phone'] ;
$message = $_REQUEST['message'] ;
require("phpmailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true; 

$mail->Username = "yoursmtp@username.com"; // SMTP username
$mail->Password = "hidden"; // SMTP password
$mail->addAttachment("uploads/".$file_name);
$mail->From = $email;
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; //SMTP port
$mail->addAddress("your@email.com", "your name");
$mail->Subject = "You have an email from a website visitor!";
$mail->Body ="
Name: $name<br>
Email: $email<br>
Telephone: $phone<br><br><br>
Comments: $message";
$mail->AltBody = $message;

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "<script>alert('Message has been sent')</script>";
?>

编辑:[已解决]我已将代码片段中的代码更新为工作代码,该代码使我可以使用PHPMailer将文件附加到电子邮件.
在执行PHPMailer脚本之前,我使用了本教程帮助将文件上传到服务器. /p>

EDIT: [SOLVED] I have updated the code in the snippets to the working code that allows me to attach a file with to an email with PHPMailer.
I used This Tutorial to help upload the file to the server before executing the PHPMailer script.

推荐答案

致电时

move_uploaded_file($file_tmp,"uploads/".$file_name);

这会在uploads/目录中创建一个文件,该文件的名称与在上传者的计算机上命名的文件的名称相同.

This creates a file in the uploads/ directory with the name of the file as it was named on the uploader's computer.

然后,您使用示例代码将附件添加到phpMailer中,因此您基本上是在尝试附加不存在的文件.

Then you used sample code to add the attachment to phpMailer so you're basically attempting to attach non-existent files.

这两行:

$mail->addAttachment('uploads/file.tar.gz');   // I took this from the phpmailer example on github but I'm not sure if I have it right.      
$mail->addAttachment('uploads/image.jpg', 'new.jpg');

应更改为:

$mail->addAttachment("uploads/".$file_name);

还请注意,如果您不希望在附件上传和发送电子邮件后保存附件,则不必致电move_uploaded_file.如果是这种情况,只需使用$_FILES['image']['tmp_name']作为文件参数调用AddAttachment.

Also note, it isn't necessary to call move_uploaded_file if you don't want to save the attachment after its uploaded and emailed. If that's the case just call AddAttachment with $_FILES['image']['tmp_name'] as the file argument.

而且,在您的HTML表单中,

Also, in your HTML form, you have

<input id="file" name="file" type="file" />

,但在代码中将输入称为image.您应该将该输入的名称从file更改为image.

but refer to the input as image in the code. You should change the name of that input from file to image.

要仅附加图像而不保存图像,请取出move_uploaded_file代码并添加:

To only attach the image and not save it take out the move_uploaded_file code and add:

$file_tmp  = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
//...
$mail->AddAttachment($file_tmp, $file_name);

这篇关于PHPMailer的文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆