 |
สอบถามการส่ง เมล์ด้วย SMTP มี 2 ตัวอย่าง แต่เลือกตัวอย่างที่ 2 แนะนำด้วยครับ ยังไม่เข้าใจ |
|
 |
|
|
 |
 |
|
ขอตอบนะครับ..
ต้องมีไฟล์ phpmailer ด้วยครับ เพราะในโค้ดมีการเรียกใช้ไฟล์ class.phpmailer.php
โหลดจากลิงค์นี้ก็ได้ครับ https://www.thaicreate.com/community/php-send-mail-smtp.html
คำถามที่ 2 ถูกต้องแล้วครับ ต้องเซ็ตข้อมูลให้เป็นของเราก่อน
|
 |
 |
 |
 |
Date :
2017-05-22 15:41:31 |
By :
sinomoney |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ส่งได้แล้ว ครับ ตามบทความนี้ http://www.picohosting.com/howto/phpmailer
ถ้าจะส่งจาก from จะส่งแบบไหนครับ ส่งจากอีกหน้าหนึ่ง มาหน้าหนึ่ง
test1.php
Code (PHP)
<form action="test2.php" method="post" enctype="multipart/form-data" name="frmMain">
<p>
เมล์ผู้ส่ง : <input name="txtEmail_from" type="text" id="txtEmail_from" class="txtbox">
<BR />
เมล์ผู้รับ : <input name="txtEmail_to" type="text" id="txtEmail_to" class="txtbox"><BR>
รายละเอียดข้อความที่ส่ง : <textarea name="txtDesc" cols="40" rows="8" class="txtbox" id="txtDesc"></textarea><BR>
<label for="fileField"></label>
ไฟล์ภาพที่ต้องการส่ง : <input type="file" name="fileField" id="fileField">
<BR>
<input type="submit" name="button" id="button" value="Submit">
<BR>
</form>
test2.php
Code (PHP)
<?PHP
require("PHPMailer_v5.0.2/class.phpmailer.php");
$mail = new PHPMailer();
$body = "ทดสอบการส่งอีเมล์ภาษาไทย UTF-8 ผ่าน SMTP Server ด้วย PHPMailer.";//ตรงนี้อยากใส่ตัวแปร txtDesc และ fileField ต้องทำอย่างไรครับ
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Host = "smtp.yourdomain.com"; // SMTP server
$mail->Port = 25; // พอร์ท
$mail->Username = "[email protected]"; // account SMTP
$mail->Password = "******"; // รหัสผ่าน SMTP
$mail->SetFrom("[email protected]", "yourname");
$mail->AddReplyTo("[email protected]", "yourname");
$mail->Subject = "ทดสอบ PHPMailer.";
$mail->MsgHTML($body);
$mail->AddAddress("[email protected]", "recipient1"); // ผู้รับคนที่หนึ่ง
$mail->AddAddress("[email protected]", "recipient2"); // ผู้รับคนที่สอง
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
|
 |
 |
 |
 |
Date :
2017-05-22 16:59:27 |
By :
sawmon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?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";
?>
|
 |
 |
 |
 |
Date :
2017-05-22 23:20:42 |
By :
ccjpn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : ccjpn เมื่อวันที่ 2017-05-22 23:35:58
รายละเอียดของการตอบ ::
Code (PHP)
ขอบคุณครับสำหรับคำแนะนำ ขอถามอีกนิดครับ
<?PHP
$t_Email=$_REQUEST['txtEmail'];//เมล์ผู้ส่ง
$t_OrderNo=$_REQUEST['txtOrderNo'];//เรื่องที่ส่ง
$t_Desc=$_REQUEST['txtDesc'];//รายละเอียด
require("PHPMailer_v5/class.phpmailer.php");
$mail = new PHPMailer();
$body = $t_Desc;
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Host = "**********"; // SMTP server
$mail->Port = 25; // พอร์ท
$mail->Username = "*********"; // account SMTP
$mail->Password = "*******"; // รหัสผ่าน SMTP
$mail->SetFrom($t_Email, $t_Email);
$mail->AddReplyTo($t_Email, $t_Email);
//เรื่องที่ส่ง
$mail->Subject = $t_OrderNo;
$mail->MsgHTML($body);
$mail->AddAddress("[email protected]", "recipient"); // ผู้รับคนที่หนึ่ง
//******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 "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Code (PHP)
//Attach file
$mail->AddAttachment($_FILES['fileField']['tmp_name'], basename($destination));
unlink($destination);
}
ถ้าเอา 2 บรรทัดนี้ออกส่งเมล์ได้ ไม่เกิด error แต่ไฟล์แนบส่งไม่ได้ เพราะอะไรครับ **ไฟล์ ที่แนบก็อยู่ใน foder uploads ครับ
ถ้าไม่เอาออก เกิด error Could not access file: /tmp/phpUXg9CB Message has been sent
|
ประวัติการแก้ไข 2017-05-23 14:22:42 2017-05-23 14:25:01 2017-05-23 14:26:27
 |
 |
 |
 |
Date :
2017-05-23 14:21:12 |
By :
sawmon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
php script ไปหาหาไฟล์ที่ upload ผิดที่น่าจะเกิดจาก php.ini: ตั้งค่า default ไปที่ upload_tmp_dir =/tmp
ลองเปลี่ยนค่าเป็น upload_tmp_dir =/uploads
comment 2 บรรทัดนี้ไว้ก่อนก็ได้
//Attach file
//$mail->AddAttachment($_FILES['fileField']['tmp_name'], basename($destination));
//unlink($destination);
}
แล้วลองดูใน โฟลดเดอร์ /uploads มีไฟล์ upload เข้ามาในโฟลดเดอร์นั้นมั๊ยครับ ถ้ามีไฟล์เข้ามาแล้วค่อยเอา comment ออกลองดูครับ
|
ประวัติการแก้ไข 2017-05-23 19:37:28
 |
 |
 |
 |
Date :
2017-05-23 19:33:34 |
By :
ccjpn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่สามารถเข้าแก้ไขไฟล์ php.ini เพราะเป็น host เช่าครับ
สามารถสร้างเป็น ไฟล์ .htaccess แล้ว อัปขึ้น ไว้ใน public_html/{ไฟล์ .htaccess ที่สร้างขึ้น}
แล้วในไฟล์ .htaccess ให้ใส่แค่ upload_tmp_dir =/uploads ใช่ไหมครับ
|
 |
 |
 |
 |
Date :
2017-05-24 11:23:05 |
By :
sawmon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|