【问题标题】:how to send email with attachments in php [duplicate]如何在php中发送带有附件的电子邮件[重复]
【发布时间】:2013-06-09 19:16:45
【问题描述】:

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

为了机制

收件人:

发件人:

主题:

消息:

附件复选框

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

<?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');
} 

?>

【问题讨论】:

  • 请考虑使用预制库进行邮寄,而不是直接使用“mail()”。使用 Mailheader-Injection,您的代码可被利用并被滥用以发送垃圾邮件。

标签: php email-attachments


【解决方案1】:

你应该看看这个页面:http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

或者,如果您的服务器支持 PEAR,那么您应该使用带有 Mail_Mime 的 PEAR Mail,这是一个比上述更简洁的解决方案。

<?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);

?>

【讨论】:

  • 你的代码中的 mail.php 和 mime.php 里面有什么
  • Mail.php 和 Mail/mime.php 是 PEAR Mail 和 Mail_Mime 包的一部分。如果您的服务器支持 PEAR,则不必将这些文件复制到脚本的文件夹中,无论如何 PHP 都会包含它们。
猜你喜欢
  • 1970-01-01
  • 2012-11-28
  • 2011-10-05
  • 1970-01-01
  • 2014-05-29
  • 2020-11-20
  • 2014-02-28
  • 1970-01-01
相关资源
最近更新 更多