【问题标题】:send email with attachment file php发送带有附件文件 php 的电子邮件
【发布时间】:2017-09-06 09:34:02
【问题描述】:

上传文件时不要在 php.ini 中发送到电子邮件。这是我的代码。

<form method="post" action="">
Name : <input type="text" name="name"><br>
Email : <input type="text" name="email"><br>
Upload File : <input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
  $image=$_FILES['file']['name'];
  $img=$_FILES['file']['tmp_name'];
					
$recipient = "test1@gmail.com";
 
    // Mail subject
    $subject = "contact by $name";
 
    // Mail content
    $email_content = " contact from $name


Name : $name
Email : $email
File : $image
";
 
    // Mail headers
    $email_headers = "From:test1@gmail.com";
 
    // Main messages
    if (mail($recipient, $subject, $email_content, $email_headers))
        {
        echo "success";
        }
      else
        {
        
		}
  ?>

姓名和电子邮件已发送到电子邮件,但上传的文件未显示在我的电子邮件中。在仅显示上传文件名的电子邮件中。如何解决这个问题。

【问题讨论】:

    标签: php jquery mysql email


    【解决方案1】:

    mail() 是一种非常基本的方法,不支持身份验证。此外,您不能通过此方法发送附件。但是,该方法的主要问题是它无法循环发送电子邮件,因为它每次发送电子邮件时都会打开和关闭套接字。

    您应该改用 swift mailer。你可以看看这个简单的教程 https://dzone.com/articles/how-to-send-emails-using-swift-mailer

    【讨论】:

      【解决方案2】:

      首先使用enctype上传二进制数据文件到服务器

      <form method="post" action="" enctype="multipart/form-data">
      

      【讨论】:

      【解决方案3】:

      你可以试试这个代码..这是一个发送带有附件的邮件的示例代码......

      <?php
      ini_set('max_execution_time', 300);
      
      $target_dir = "uploads/";
      $target_file= $target_dir .time().basename($_FILES['attachment']['name']); 
      $movefile=move_uploaded_file($_FILES['attachment']['tmp_name'], $target_file);
          $username=$_POST['Name'];
          $email=$_POST['email'];
          $website=$_POST['website'];
          $comments=$_POST['comments'];
      
      $to = "receiver@gmail.com";
      $subject= "Mail from";
      
        $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
      
               $headers = "From:<example@gmail.com>"."\r\n" .
               "MIME-Version: 1.0\r\n" .
                  "Content-Type: multipart/mixed;\r\n" .
                  " boundary=\"{$mime_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" .
                  "Contact Details"."\n".
               "Name :". $_POST['Name']."\n".
      
      
      "\n\n";
      
               foreach($_FILES as $userfile)
               {
                  $tmp_name = $userfile['tmp_name'];
                  $type = $userfile['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
                  $name = $userfile['name'];
                  $size = $userfile['size'];
      
                  if (file_exists($tmp_name))
                  {
                     if(is_uploaded_file($tmp_name))
                     {
                        $file = fopen($tmp_name,'rb');
      
                        $data = fread($file,filesize($tmp_name));
      
                        fclose($file);
      
      
                        $data = chunk_split(base64_encode($data));
                     }
      
                     $message .= "--{$mime_boundary}\n" .
                        "Content-Type: {$type};\n" .
                        " name=\"{$name}\"\n" .
                        "Content-Disposition: attachment;\n" .
                        " filename=\"{$fileatt_name}\"\n" .
                        "Content-Transfer-Encoding: base64\n\n" .
                     $data . "\n\n";
                  }
               }
      
               $message.="--{$mime_boundary}--\n";
      
      if (mail($to, $subject, $message, $headers)){
         echo "<script>alert('Mail sent Successfully');</script>";
                  echo "<script>window.location = 'contact.php';</script>";
      
              } else {
                  echo "<script>alert('Mail Not Send');</script>";
                  echo "<script>window.location = 'contact.php';</script>";
      
      
              } 
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2012-06-15
        • 1970-01-01
        • 2020-11-20
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多