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

如何使用带有附件的PEAR邮件包通过PHP发送电子邮件,php,email,pear,mail-mime,Php,Email,Pear,Mail Mime,我试图通过使用带有附件的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

我试图通过使用带有附件的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
对象中的头添加到
$headers

$attachmentheaders = $mime->headers($headers);
然后更改您的邮件呼叫:

$mail = $smtp->send($to, $attachmentheaders, $body);
下面的教程可能会有所帮助:

我没有检查此代码,因此如果不工作,我很抱歉

包括“Mail.php”;
包括“Mail/mime.php”;
$text='电子邮件的文本版本';
$html='电子邮件的html版本';
$file='/home/richard/example.php';
$crlf=“\n”;
$hdrs=阵列(
'来自'=>'you@yourdomain.com',
'主题'=>'测试mime消息'
);
$host=“host”;
$port=“25”;
$username=“username”;
$password=“password”;
$mime=新邮件\u mime(数组('eol'=>$crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file,'text/plain');
$body=$mime->get();
$hdrs=$mime->headers($hdrs);
$mail=&mail::工厂('smtp',
数组('host'=>$host,
“端口”=>$port,
“auth”=>正确,
“用户名”=>$username,
“密码”=>$password));
$mail->send($mail)postmaster@localhost“,$hdrs,$body);

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

/**
  *  
  * @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);
}
/**
*  
*@param type$recipient
*@param type$subject
*@param type$消息
*@param type$附件
*  
*要使此邮件正常工作,我需要执行以下操作:
*pear安装邮件
*pear安装网络\u SMTP
*pear安装邮件\u Mime
* 
*/
公共函数sendmail($recipient、$subject、$message、$attachment=''){
需要一次“Mail.php”;
需要一次“Mail/mime.php”;
$from=“调度员”;
$host=“smtp.mymailserver.com”;
$port=“587”;
$username=“mailuser”;
$password=“password”;
$headers=array('From'=>$From',To'=>$recipient',Subject'=>$Subject);
如果($attachment!=''){
$crlf=“\n”;
$mime=新邮件\u mime($crlf);
$mime->setTXTBody($message);
$mime->addAttachment($attachment,'application/pdf');
$body=$mime->get();
$headers=$mime->headers($headers);
}否则{
$body=$message;
}
$smtp=Mail::工厂('smtp',
数组('host'=>$host,
“端口”=>$port,
“auth”=>正确,
“用户名”=>$username,
“密码”=>$password));
$smtp->send($recipient,$headers,$body);
}


在哪里向邮件中添加$mime?这就是我的全部资料,如果需要添加,那么我应该在哪里添加。我尝试了这个方法,效果很好。但现在我有一个警告:常数只能计算为C:\xampp\php\PEAR\Mail\mime.php中的标量值,第1089行您可以使用@check-line-by-line查看警告抛出的位置。这并不能解释作者的错误,因此它不能回答问题。这只是共享工作代码。然而,代码很容易阅读。这并不能解释作者哪里错了,所以它不能回答问题。这只是共享工作代码。尽管如此,代码还是很难阅读。
/**
  *  
  * @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);
}
<?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);
?>