<?php
$email_to = $_REQUEST['txtEmail_to'] ;
$message = $_REQUEST['txtDesc'] ;
require("PHPMailer_v5.0.2/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.example.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "[email protected]"; // "[email protected]";
// below we want to set the email address we will be sending our email to.
$mail->AddAddress($email_to, "Mr.XYZ");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
//AddAttachment File : upload local file to server and attatch from server path
if (isset($_FILES['fileField']) &&
$_FILES['fileField']['error'] == UPLOAD_ERR_OK) {
//check MIME TYPE อนุญาติเฉพาะนามสกุลที่กำหนด
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['fileField']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
$destination = sprintf('./uploads/%s.%s', sha1_file($_FILES['fileField']['tmp_name']), $ext);
//move the file to a temp folder
if (!move_uploaded_file($_FILES['fileField']['tmp_name'], $destination)) {
throw new RuntimeException('Failed to move uploaded file.');
}
//Attach file
$mail->AddAttachment($_FILES['fileField']['tmp_name'], basename($destination));
//delete the file (ถ้าไม่ลบจะมีไฟล์อยู่ที่ server เพิ่มขึ้นเรื่อย)
unlink($destination);
}
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
ถ้าเอา 2 บรรทัดนี้ออกส่งเมล์ได้ ไม่เกิด error แต่ไฟล์แนบส่งไม่ได้ เพราะอะไรครับ **ไฟล์ ที่แนบก็อยู่ใน foder uploads ครับ
ถ้าไม่เอาออก เกิด error Could not access file: /tmp/phpUXg9CB Message has been sent