2013-12-13 56 views
-2

我希望你能幫助我解決我的問題,因爲我真的需要我的網站。 如何添加將文件附加到我用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 

} 

?> 
+0

可能的重複[如何添加在PHP的在線郵件上附加文件的能力?](http://stackoverflow.com/questions/20529650/how-do-i-add-the-ability-對附加-A-文件上-A-在線郵件功能於PHP) – vstm

回答

1
<?php 
    $filename = "form.txt"; //attach filename 
    $to = "[email protected]"; //To 
    $from = "[email protected]"; //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 
?> 
2

那麼對於PHP複雜收發郵件檢出PHPMailer類。

使用PHPMailer中的函數發送複雜的電子郵件(例如附件)要容易得多。

相關問題