user3385236 user3385236 - 1 year ago 100
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.