2

Have worked with PHPMailer for 4 Hours now, it's works fine and send the mail. But when i attach a file - then file is not included in the mail i get.

I have only added a file to the "File1", can someone see/say whats wrong and why i don't get the attached file, when i get a mail from the script ?

I have tried with:

$mail->AddAttachment($_FILES['file1']['tmp_name'], $_FILES['file1']['name']);

and

$mail->AddAttachment($file1);

Without any luck to attach the file.

My code is:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = false;
$mail->Host = 'smtp.curanet.dk';
$mail->Port = 25;
$mail->SetFrom('thomas@test.dk', 'SJ');
$mail->AddAddress('thomas@test.dk', 'SJ');
$mail->Subject = 'Henvendelse fra Hjemmeside';

$besked1 = "Der er flg. henvendelse fra hjemmesiden ang. Tilbud: \n";
$besked1 .= "Navn: " . $_POST['fromname']. "\n";
$besked1 .= "Telefon: " . $_POST['fromtlf']. "\n";
$besked1 .= "E-mail: " . $_POST['fromemail']. "\n";
$besked1 .= "Start dato: " . $_POST['begin_day']. ". " . $_POST['begin_month']. " " . $_POST['begin_year']. "\n";
$besked1 .= "Slut dato: " . $_POST['complete_day']. ". " . $_POST['complete_month']. " " . $_POST['complete_year']. "\n";
$besked1 .= "Budget: " . $_POST['building_budget']. " kr.". "\n";
$besked1 .= "Besked: " . $_POST['frommsg'];

$mail->Body = $besked1; 
$mail->IsHTML(false);

$mail->AddAttachment($_FILES['file1']['tmp_name'], $_FILES['file1']['name']);
$mail->AddAttachment($_FILES['file2']['tmp_name'], $_FILES['file2']['name']);
$mail->AddAttachment($_FILES['file3']['tmp_name'], $_FILES['file3']['name']);
/*$mail->AddAttachment($file1);
$mail->AddAttachment($file2);
$mail->AddAttachment($file3);*/

if($mail->send())
{
header('Location: tak.php');
} else {
    echo "<script>alert('Mailer Error: " . $mail-> ErrorInfo."')</script>";
}
Thomas Bøg Petersen
  • 1,157
  • 2
  • 22
  • 57
  • Did you check if the $_FILES is set? "print_r($_FILES);" could be that the upload is not working? – Erik255 Jan 23 '14 at 14:26
  • Hi Erik, if i place the "print_r($_Files); before my "header('Location: tak.php'); then i get an "Array()". – Thomas Bøg Petersen Jan 24 '14 at 07:09
  • That means there are no files available for php... that explains why you can't send them. You can find details and example here : http://www.php.net/manual/en/features.file-upload.multiple.php – Erik255 Jan 24 '14 at 08:16

1 Answers1

2

The problem is that you want to pass the file directly:

$mail->AddAttachment($_FILES['file1']['tmp_name'], $_FILES['file1']['name']);

However you will have to save the file and then link it:

$mail->AddAttachment("path/to/file");

edti: At least this is the problem I had before, temp location was not an option.

Boris
  • 802
  • 1
  • 10
  • 13
  • Was also my first thought. But there is a stackoverflow issue where it seems to work that way, but i din't try it http://stackoverflow.com/questions/17776126/add-attachment-through-phpmailer – Erik255 Jan 23 '14 at 14:28
  • @Erik255 Correct! But for some reason it didn't work for me at the time. Not sure why. – Boris Jan 23 '14 at 14:30