Question

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>";
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top