Sending attachment using email as file name with Phpmailer

Asked

Viewed 294 times

0

I am using Phpmailer to send a form and I would like the attached files when they arrived in the email destination, had the file name the email of the person from whom I send, and not the file name, I did several tests here but could not.

<input type="file" name="arquivo[]" multiple>

$email = strip_tags(trim($_POST['email']));
$arquivo = $_FILES['arquivo'];

if ($arquivo['size'][0] != 0) {
    for ($i = 0; $i < count($arquivo ['tmp_name']); $i++) {
        $mail->AddAttachment($arquivo ['tmp_name'][$i], $arquivo['name'][$i]);
    }
}
  • What is your problem?

  • When I put the code like this, it works $mail->Addattachment($file ['tmp_name'][$i], 'file name') But when I try $mail->Addattachment($file ['tmp_name'][$i], $email[$i]); the file only takes the first letter of the person’s email

1 answer

0

The code $email[$i] will only take the first character of the string. In the case of for, when the variable $i is equal to 0, will take the first character (the 0 represents the index of the first character of a string). Example:

$str = "Olá";
$str[0] -> O
$str[1] -> l
$str[2] -> á

Put only the variable $email concatenating with the variable $i, since the $email is not part of the array, it is a fixed value variable:

$mail->AddAttachment($arquivo ['tmp_name'][$i], $email.$i);
  • It is printing the index in the photo [email protected], [email protected], [email protected] and so on. When I use only the variable name the email is sent but does not arrive at the destination, and in my email I receive an error message that cannot be sent

  • What default name you want to give to the files?

  • If you only use the $email variable in the name, the loop will overwrite existing files as the same name, so it has q differentiate one from the other.

  • I want the file to come with the person’s email, I did a test putting for example 'test name' instead of the variable and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.