使用PHPMailer发送电子邮件-创建并附加.txt

时间:2018-09-03 11:39:28

标签: php email phpmailer email-attachments

我有一个表单,我想将从表单接收的数据放入.txt文件并附加到邮件中。无需保存到服务器。 如何创建此文件并将其附加到邮件? 在查看PHPMailer随附的示例时,我找不到我的问题的答案。

< php
if ($_POST[ 'message' ] == '' ) {
	$nosketch = '<p>The user has not provided sketch.</p>';
} else {
	$body .= "\n\n--" . $bound . "\n";
	$attachment = $_POST[ 'message' ];
	$file_name = "mol_" . time() . ".txt";
	$body .= "Content-Type: text/plain; name=" . $file_name . "\n";
	$body .= "Content-Transfer-Encoding: 8bit\n";
	$body .= "Content-Disposition: attachment\n\n";
	$body .= $attachment . "\n";
	$body .= "--" . $bound . "--\n\n";
	$mailer->AddAttachment();
};
require 'mailer/PHPMailerAutoload.php';
$mailer = new PHPMailer;
$mailer->setFrom( $_POST[ 'email1' ], $_POST[ 'name' ] );
$mailer->addAddress( 'name1@email.com' );
$mailer->AddCC( 'name1@email.com' );
$mailer->Subject = 'User ' . $_POST[ 'name' ] . ' submit';
$mailer->isHTML( true );
$mailer->Body = $_POST[ 'name' ] . '<br/>' . $_POST[ 'message' ];
$mailer->send();
?>

2 个答案:

答案 0 :(得分:2)

您可以使用phpMailer lambda

的这种方法

例如

addStringAttachment($string,$filename,$encoding,$type)

这里是the documentation on that function

希望它能起作用

答案 1 :(得分:0)

我更改了密码

< php
if ($_POST[ 'message' ] == '' ) {
	$nosketch = '<p>The user has not provided sketch.</p>';
} else {
	$string = $_POST['message'];
	$filename = "mol_" . time() . ".txt";
	$encoding = "7bit";
	$type  = "text/plain";
	$disposition = "attachment";
};
require 'mailer/PHPMailer.php';
$mailer = new PHPMailer;
$mailer->setFrom( $_POST[ 'email1' ], $_POST[ 'name' ] );
$mailer->addAddress( 'name1@email.com' );
$mailer->AddCC( 'name1@email.com' );
$mailer->Subject = 'User ' . $_POST[ 'name' ] . ' submit';
$mailer->isHTML( true );
$mailer->Body = $_POST[ 'name' ] . '<br/>' . $_POST[ 'message' ];
$mailer->addStringAttachment($string,$filename,$encoding,$type,$disposition);
$mailer->send();
?>

但是,当我尝试发送消息时,脚本会落在require 'mailer/PHPMailer.php';上。如果我使用旧文件,则会发送该消息。 我也有'img1'问题。如果文本显示在网页上,则该文本是正确的。但是,当文本插入文件时,它具有两个或多个段落。