L
L
Let_peace2021-11-19 01:52:14
PHP
Let_peace, 2021-11-19 01:52:14

How to attach a file from the server to a letter using phpmailer?

Sending an email using phpmailer. I'm trying to attach a file that is on the server. I tried several methods that I found on the Internet:

// $path - абсолютный путь до файла
$path = "https://mysite.ru/files/file.txt": // в таком виде задается
$mail->AddAttachment($path);
$mail->addAttachment(file_get_contents($path));
$mail->addStringAttachment(file_get_contents($path));
$mail->addStringAttachment(file_get_contents($path), "file");
// и всякие другие

The file is still not attached. The path to the file is correct.
Please help me to solve this problem...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ipatiev, 2021-11-19
@Let_peace

To properly address this issue, you need to master two important concepts. No, three.
1. Start distinguishing files from virtual addresses . Not that this is necessarily the cause, but specifying a URL instead of a file path is incorrect.
2. Master such a thing as debugging . If something does not work, it most likely writes errors. Mistakes must be read to correct. There is no magic code that always works in general, you just have to "find it on the Internet". Any code may not work for a million reasons beyond its control.
An important takeaway from this is that you don't need to know how to attach files.You already know this. You need to learn to see PHP errors so that you can read them, google them, and eventually fix them.
3. Learn English. And not just randomly copy the code found on the Internet, but also try to figure out what it does. This can be seen even from the names of the functions. And thus discard obviously non-working options like $mail->addAttachment(file_get_contents($path));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question