我正在尝试使用带有附件的 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>");
}
?>