如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件

Posted

技术标签:

【中文标题】如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件【英文标题】:How to send emails with PHP using the PEAR Mail package with attachment 【发布时间】:2012-08-01 19:32:54 【问题描述】:

我正在尝试使用带有附件的 PEAR 邮件包发送一封带有 php 的电子邮件。该电子邮件使用我从互联网上获得的代码成功发送。但是,不会发送或附加附件。我哪里出错了,下面是我的代码。

<?php

require_once "Mail.php"; 
require_once "Mail/mime.php";

$from = "<my.name@company.com>";
$to = "<myname@gmail.com>";
$subject = "Testing email from PHP with attachment";
$body = "Testing email from PHP with attachment";
$file = "invoices/PRINV7_3.pdf";
$host = "host";
$port = "25";
$username = "username";
$password = "password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);

$mime = new Mail_mime();

if ($mime->addAttachment($file,'application/pdf'))
    echo "attached successfully! </br>";
 else 
    echo "Nope, failed to attache!! </br>";


$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) 
  echo("<p>" . $mail->getMessage() . "</p>");
  else 
  echo("<p>Message successfully sent!</p>");
 

?>

【问题讨论】:

你在哪里添加 $mime 到邮件消息? 这就是我所拥有的,如果需要添加,我应该在哪里添加它。 【参考方案1】:

我没有检查这个代码,所以如果不工作我很抱歉

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'
);
$host = "host";
$port = "25";
$username = "username";
$password = "password";

$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('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));
$mail->send('postmaster@localhost', $hdrs, $body);

【讨论】:

我试过了,效果很好。但现在我有这个警告警告:常量只能计算为第 1089 行 C:\xampp\php\PEAR\Mail\mime.php 中的标量值 您可以使用@check 逐行禁止查看警告的抛出位置。 这并没有解释作者哪里错了,所以它没有回答这个问题。这只是共享工作代码。但是,代码很容易阅读。【参考方案2】:

我相信您需要将 $mime 对象中的标头添加到您的 $headers

$attachmentheaders = $mime->headers($headers);

然后更改您的邮件呼叫:

$mail = $smtp->send($to, $attachmentheaders, $body);

这里有一个可能会有所帮助的教程: http://www.html-form-guide.com/email-form/php-email-form-attachment.html

【讨论】:

【参考方案3】:
<?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);
?>

【讨论】:

【参考方案4】:

这是来自我的一个应用程序的工作代码:

/**
  *  
  * @param type $recipient
  * @param type $subject
  * @param type $message
  * @param type $attachment
  *  
  * To make this mail PEAR work, I needed to do:
  * pear install mail
  * pear install Net_SMTP
  * pear install Mail_Mime
  * 
*/

public function sendmail($recipient, $subject, $message, $attachment = '') 
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Dispatcher <server@mymailserver.com>";
$host = "smtp.mymailserver.com";
$port = "587";
$username = "mailuser";
$password = "password";

$headers = array ('From' => $from, 'To' => $recipient, 'Subject' => $subject);

if ($attachment != '') 
  $crlf = "\n";
  $mime = new Mail_mime($crlf);
  $mime->setTXTBody($message);
  $mime->addAttachment($attachment, 'application/pdf');
  $body = $mime->get();
  $headers = $mime->headers($headers);
 else 
  $body = $message;


$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$smtp->send($recipient, $headers, $body);

【讨论】:

这并没有解释作者哪里错了,所以它没有回答这个问题。这只是共享工作代码。即使除此之外,代码也很难阅读。

以上是关于如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

PHP Pear Mail 无法发送带有 Unicode 字符的名称

使用 PEAR Mail 包发送邮件时出错

带有附件问题的 PHP Pear Mime 邮件

如何使用 System.NET.mail 发送带有附件但没有任何纯文本正文的电子邮件?

Java Mail 发送带有附件的邮件

如何安装软件包 mail 和 mail_mime (pear)