user3385236 user3385236 - 2 years ago 128
PHP Question

Add date to attachment in PHP mailer SMTP

I am trying to add the date to a file which is emailed using SMTP in php. What i need is to add the date to the file name sent in the email, without changing the file name on the server. So my code still looks for 'file.csv' but names it '(date)file.csv' in the email attachment.


I have already tried.

$today = date("Y-m-d");
$filename = 'file'.$today.'csv'
$mail->addAttachment('./'.$filename, 'file.csv', 'base64', 'text/csv');


$today = date("Y-m-d");
$filename = "file.csv";
$filename2 = 'file'.$today.'csv';
$mail->addAttachment($filename, $filename2);

Answer Source

It's not quite a duplicate as you know how to set both names, but you've simply got your filenames the wrong way around. Try this:

$today = date("Y-m-d");
$filename = 'file'.$today.'csv'
$mail->addAttachment('./file.csv', $filename, 'base64', 'text/csv');

The docs on this method are here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download