PHP:如何使用 smtp 设置发送带附件的电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3299577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:16:02  来源:igfitidea点击:

PHP: How to send email with attachment using smtp settings?

phpsmtpemailemail-attachments

提问by Naveed

I am sending emails successfully using following code. But now I want to attach a text file (example: test.txt) with email. Any Idea?

我正在使用以下代码成功发送电子邮件。但现在我想在电子邮件中附加一个文本文件(例如:test.txt)。任何的想法?

require_once "Mail.php";

$from = "Usman <[email protected]>";
$to = "Naveed <[email protected]>";
$subject = "subject";
$body = "";

$host = "smtp.gmail.com";
$username = "username";
$password = "password";

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

$smtp = Mail::factory( 'smtp', array('host' => $host,
          '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>" );
}

回答by Maerlyn

Found this code as one of the first hits of the google://pear mail attachmentsearch.

发现此代码是google://pear mail attachment搜索的第一个命中之一。

include('Mail.php');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './files/example.zip';
$hdrs = array(
              'From'    => '[email protected]',
              'To'      => '[email protected]',
              'Subject' => 'Test mime message'
              );

$mime = new Mail_mime();

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$mime->addAttachment($file,'application/octet-stream');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail', $params);
$mail->send('[email protected]', $hdrs, $body); 

回答by apiri

If you additionally make use of the PHP PEAR Mail_Mimemodule it provides the appropriate handling and encoding to incorporate attachments as part of your email.

如果您另外使用 PHP PEAR Mail_Mime模块,它会提供适当的处理和编码以将附件合并为电子邮件的一部分。

回答by swisswiss

here is the code you're looking for:

这是您正在寻找的代码:

<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge

$from = "Robert Davis <[email protected]>";
$to = "Sam Hill <[email protected]>";
$subject = 'Test mime message with an attachment';

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

$text = 'Text version of email';// text and html versions of email.
$html = '<html><body>HTML version of email. <strong>This should be bold</strong></body>        </html>';

$file = './sample.txt'; // attachment
$crlf = "\n";

$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');

//do not ever try to call these lines in reverse order
$body = $mime->get();
$headers = $mime->headers($headers);

$host = "sasl.smtp.pobox.com";
$username = "[email protected]";
$password = "Kdu48Adi3";

$smtp = Mail::factory('smtp', array ('host' => $host, '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>");
}
?>

回答by TheHippo

Sending email with PHP always feels a little bit like a struggle. If you are able to use them I would recommend one of these two mail libraries for PHP:

使用 PHP 发送电子邮件总是感觉有点像挣扎。如果您能够使用它们,我会为 PHP 推荐以下两个邮件库之一:

回答by Gazillion

You seem to be using the PEAR Mail package.

您似乎正在使用 PEAR Mail 包。

Take a look at the Mail_Mine object which does what you're trying to do and has an easy way of adding attachments (simply call addAttachments).

看一看 Mail_Mine 对象,它可以完成您想要做的事情,并且有一种添加附件的简单方法(只需调用 addAttachments)。

http://pear.php.net/manual/en/package.mail.mail-mime.php

http://pear.php.net/manual/en/package.mail.mail-mime.php