如何在php中发送附件的电子邮件

时间:2013-06-13 11:55:46

标签: php email-attachments

可以帮我一个代码,它可以发送带有.dox .docx .pdf .jpeg的两个附件的邮件,它们已经在服务器上并且链接路径保存在数据库(MySql)中了吗?

为了机制

要:

自:

主题:

消息:

检查附件盒

我只是一个初学者我只知道如何发送邮件我们出了像贝娄这样的附件

<?php 
$errors = '';
$myemail = 'admin@mydomain.net'
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');
} 

?>

1 个答案:

答案 0 :(得分:0)

您应该查看此页面:http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

或者如果您的服务器支持PEAR,那么您应该将PEAR Mail与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'    => 'you@yourdomain.com',
          '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('postmaster@localhost', $hdrs, $body);

?>