-1

I tried to attach my local file using PHP Mailer. I'm getting attachment only if the attachment file is in own server, but when I tried to attach the file from my c drive say [C:\Users\emp10144\Downloads], am getting attachment but with blank page. Did I need to modify my codings. Below is the codings I have used.

$mail->From = 'admin123@sampledemos123.online';
$mail->FromName = 'Admin';
$mail->AddAddress('targetmail@gmail.com', 'User');  // Add a recipient
//$mail->AddAddress('ellen@example.com');               // Name is optional
//$mail->AddAttachment('Daily_Milk_Report.csv','Daily_Milk_Report.csv'); 
This is working fine as the attcahment file is in own server
$filename = "C:\Users\emp10144\Downloads','Daily_Milk_Report.csv"; // Need to attcah this file from C drive/folder.
//$string = file_get_contents("C:\Users\emp10144\Downloads\sample.pdf");
$mail->AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/vnd.ms-excel');
$mail->IsHTML(true);                                  // Set email format to HTML
Hari
  • 7
  • 3
  • You should attach a file which is accessible by others on the internet. so it should be first uploaded to a server and then can be attached. – parisssss Jun 03 '19 at 07:20
  • Thanks @parisssss , but I learnt that we can attach remote files by using the function "$mail->AddStringAttachment" . – Hari Jun 03 '19 at 07:23
  • Can anyone help me on how to solve this issue as am working for the same for past 2 days... – Hari Jun 03 '19 at 07:43
  • Be specific about what you actually need/want here, please. `C:\Users\emp10144\Downloads` is not what would be considered a “remote file” to begin with. _“Need to attcah this file from C drive/folder.”_ - of the server? Or the user’s machine? If the latter, you need to make the user upload the file, you can not access arbitrary files on the client. – 04FS Jun 03 '19 at 07:46
  • @04FS, need to attach the files from user (my machine), but using AddStringAttachment, can we call the needed file and send it as an attachment? – Hari Jun 03 '19 at 07:51
  • Then you need to upload the file to the server first. https://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php – 04FS Jun 03 '19 at 07:53
  • Okay, I'm trying to convert json to csv and trying to send as attachment using php mailer, and this is my jquery code for downloading `"link.download = fileName + ".csv";`. Instead of downloading in my machine can i download the file in my server. Is this possible one? – Hari Jun 03 '19 at 07:59

1 Answers1

-1

No, you cannot pass a URL to addAttachment and have it fetch the resource.

This is intentional; PHPMailer is not an HTTP client, and actively avoids being one. If you want to do this, you need to take responsibility for the fetch yourself, which is most easily achieved like this:

$mail->addStringAttachment(file_get_contents($url, 'myfile.png'));
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/194407/discussion-on-answer-by-synchro-how-to-attach-remote-files-to-email-using-phpmai). – Samuel Liew Jun 04 '19 at 05:45