2013-06-13 56 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