hello...

i am doing mail concepts. in this i want to send a dynamic attachments with email. actually user gets email with attachment. this attachment will be dynamic. how to create dynamic attachment for email.i tried this. but no use. anybody help.

i have 'attachments.txt'file with content

User Name: <<name>>
Designid:<<designid>>
Designname:<<designname>>
Orderid:<<orderid>>

i was updated this file as

$myfile = "attachments.txt";
$fh = fopen($myfile, 'w') or die("can't open file");
$stringData = str_replace("<<name>>", $fetuser[firstname], $fh);
//echo $fetuser[firstname];exit;
fwrite($fh, $stringData);
$stringData = str_replace("<<designid>>",$_SESSION[designid], $fh);
fwrite($fh, $stringData);
$stringData = str_replace("<<designname>>", $fetch_designname[designtype], $fh);
fwrite($fh, $stringData);
$stringData = str_replace("<<orderno>>", $ordno, $fh);
fwrite($fh, $stringData);
fclose($fh);

finally i was added attachment in mail like this $mail->AddAttachment("$myfile");

but this is not working properly. anybody help.

Recommended Answers

All 2 Replies

hi, you can try this one:

<?php
 
// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
 
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}
 
// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>

here's the link : http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/

thanks for you reply. i want attachment template.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.