1

I have a form and I want to place the received data from the form into a .txt file and attach to the mail. Without saving to the server. How to create and attach this file to the mail? Looking at the example that comes with PHPMailer I do not find the answer to my question.

< 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();
?>
Sined
  • 11
  • 2

2 Answers2

2

You can use this method of phpMailer addStringAttachment($string,$filename,$encoding,$type)

e.g.

$mail = new PHPMailer();
$mail->addStringAttachment($string,$filename,$encoding,$type);

Here is the documentation on that function.

hope it works

Synchro
  • 35,538
  • 15
  • 81
  • 104
Exterminator
  • 1,221
  • 7
  • 14
0

I changed the code

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

But when I try to send a message, script drop down on require 'mailer/PHPMailer.php';. If I use the old file, the message is sent. And I have also a problem with 'img1'. If the text is displayed on a web page, then it is in line. But when text is inserted into a file, it has two or more paragraphs.

Sined
  • 11
  • 2