开发者

create dynamic attachments using php

开发者 https://www.devze.com 2023-01-31 22:23 出处:网络
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 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);
开发者_JS百科

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

but this is not working properly. anybody help.


I don't think you can pass a file handler to str_replace like that. Read in the contents first, replace your placeholders, and then write the file again. Also, if you write this to the same file containing your template, it will no longer be a template the second time you want to send an email, but I'm sure you thought of something for that :)

EDIT:

So, you can do someting like this:

$text = file_get_contents($templateFile);

// perhaps some error handling here

$text = str_replace(array('<<name>>', '<<designid>>', '<<designname>>', '<<orderid>>'),
                    array($fetuser[firstname], $_SESSION[designid], $fetch_designname[designtype], $ordno),
                    $text);

file_put_contents($attachmentFile, $text);


Why are you bothering to write the file back to disk? It's not necessary.

$mail->AddAttachment("$myfile");

Can you give us a clue what $mail is instantiated from?

but this is not working properly

That's a meaningless statement. Please provide error messages or details of how the results differ from what you were expecting.

0

精彩评论

暂无评论...
验证码 换一张
取 消