簡體   English   中英

如何使用帶附件的 PEAR Mail 包通過 PHP 發送電子郵件

[英]How to send emails with PHP using the PEAR Mail package with attachment

我正在嘗試使用帶附件的 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>");
 }

?>

我還沒有檢查過這段代碼,所以如果不起作用,我很抱歉

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

我相信您需要將$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

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

這是我的一個應用程序的工作代碼:

/**
  *  
  * @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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM