如何将附加文件添加到我的在线电子邮件PHP?

时间:2013-12-13 08:00:29

标签: php

我希望你能帮我解决我的问题,因为我的网站确实需要它。 我如何添加将文件附加到我用PHP编写的在线电子邮件的可能性? :)

以下是我想将其添加到的代码:

<?php 
session_start();

if( isset($_POST['submit'])) 
{
     if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
       {  
       $to=$_POST["to"];
       $from=$_POST["from"];
       $name=$_POST["name"];
       $subject=$_POST["subject"];
       $message=$_POST["message"];
       $message=$message."\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
       $head="From: ".$from."\r\n".'Reply-To: '.$From."\r\n";
       $her=$head.' < '.$from.' >';
       $techomag=mail($to, $subject, $message, $her);
       echo "Your Message Has been send successfully...:)..";
           unset($_SESSION['security_code']);
      echo( '<a href="mysite.com/index.php">Click here to send another email</a>' );
      } 
      else 
      {

        echo 'Sorry, you have provided an invalid captcha security code :(...';
        echo( '<a href="mysite.com/index.php">Click here to Go back and try once more</a>' );
      }

} 
else 
{
?>
<?php
?>


<div style="height:10px; clear:both"></div>

<form action="index.php" method="post">
   <table border="1"><tbody>
   <tr><td>To Email :</td><td><input name="to" type="text" /></td></tr>
   <tr><td>From Email :</td><td><input name="from" type="text" /></td></tr>
   <tr><td>From Name :</td><td><input name="name" type="text" /></td></tr>
   <tr><td>Subject :</td><td><input name="subject" type="text" /></td></tr>
   <tr><td>Message :</td><td><textarea cols="30" rows="10" name="message"></textarea></td></tr>
   <tr><td><img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
   <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" />
   <input type="submit" name="submit" value="Send Email!" /></td></tr>
   </tbody></table>
</form>

<?php

}

?>

2 个答案:

答案 0 :(得分:2)

使用PHP检查PHPMailer类的复杂电子邮件。

使用PHPMailer中的函数发送复杂的电子邮件(例如附件)要容易得多。

答案 1 :(得分:1)

<?php
  $filename = "form.txt"; //attach filename
  $to = "abc@mail.ru"; //To
  $from = "def@gmail.com"; //From
  $subject = "Test"; //Subj
  $message = "Текстовое сообщение"; //Message
  $boundary = "---"; //Delimitter
  /* Headers */
  $headers = "From: $from\nReply-To: $from\n";
  $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
  $body = "--$boundary\n";
  /* Text message */
  $body .= "Content-type: text/html; charset='utf-8'\n";
  $body .= "Content-Transfer-Encoding: quoted-printablenn";
  $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
  $body .= $message."\n";
  $body .= "--$boundary\n";
  $file = fopen($filename, "r"); //Открываем файл
  $text = fread($file, filesize($filename)); //Считываем весь файл
  fclose($file); //Закрываем файл
  /* ADD attach */
  $body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($filename)."?=\n"; 
  $body .= "Content-Transfer-Encoding: base64\n";
  $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($filename)."?=\n\n";
  $body .= chunk_split(base64_encode($text))."\n";
  $body .= "--".$boundary ."--\n";
  mail($to, $subject, $body, $headers); //SEND
?>