gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:29:30 26 4
gpt4 key购买 nike

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

require_once "Mail.php";

$from = "Usman <from@example.com>";
$to = "Naveed <to@example.com>";
$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>" );
}

最佳答案

发现此代码是 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' => 'someone@domain.pl',
'To' => 'someone@domain.pl',
'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('mail@domain.pl', $hdrs, $body);

关于PHP:如何使用 smtp 设置发送带附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com