2013-06-13 63 views
1

可以幫助我一個代碼,它可以發送郵件的兩個附件只有一個.dox.docx .pdf .jpeg只有一次已經在服務器上,鏈接路徑被保存在數據庫(MySql)?如何發送電子郵件與附件在php

對於機制的緣故

要:

來源:

主題:

消息:

複選框附件

只是個初學者我只知道如何發送郵件我們附件就像婁

<?php 
$errors = ''; 
$myemail = '[email protected]' 
if(empty($_POST['name']) || 
    empty($_POST['email']) || 
    empty($_POST['message'])) 
{ 
    $errors .= "\n Error: all fields are required"; 
} 

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address)) 
{ 
    $errors .= "\n Error: Invalid email address"; 
} 

if(empty($errors)) 
{ 
    $to = $myemail; 
    $email_subject = "New Mail From: $name"; 
    $email_body = "$message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address"; 

    mail($to,$email_subject,$email_body,$headers); 
    //redirect to the 'thank you' page 
    header('Location: thank-you.html'); 
} 

?> 
+0

請考慮使用預製庫來郵寄,而不是直接使用「mail()」。使用Mailheader-Injection,您的代碼可被利用,並可能被濫用發送垃圾郵件。 –

回答

0

你應該看看這個頁面:http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

或者,如果你的服務器支持PEAR,那麼你應該使用PEAR郵件與Mail_Mime,它是一個更清潔的解決方案,然後上面。

<?php 

include 'Mail.php'; 
include 'Mail/mime.php' ; 

$text = 'Text version of email'; 
$html = '<html><body>HTML version of email</body></html>'; 
$file = '/home/richard/example.php'; 
$crlf = "\n"; 
$hdrs = array(
      'From' => '[email protected]', 
      'Subject' => 'Test mime message' 
     ); 

$mime = new Mail_mime(array('eol' => $crlf)); 

$mime->setTXTBody($text); 
$mime->setHTMLBody($html); 
$mime->addAttachment($file, 'text/plain'); 

$body = $mime->get(); 
$hdrs = $mime->headers($hdrs); 

$mail =& Mail::factory('mail'); 
$mail->send('[email protected]', $hdrs, $body); 

?> 
+0

whats裏面的mail.php和mime.php在你的代碼 – madcoder

+0

Mail.php和Mail/mime.php是PEAR Mail和Mail_Mime包的一部分。如果您的服務器支持PEAR,則不必將這些文件複製到您的腳本文件夾,PHP將包括它們。 – langm