\

Facebook


วันพฤหัสบดีที่ 13 พฤศจิกายน พ.ศ. 2557

[PHP] ส่งเมลล์ผ่าน Google ด้วย PHPMailer [อัพเดต2014]


อันดับแรกให้ไปดาว์นโหลด PHPMailer เว่อร์ชั่นล่าสุดมาก่อนครับ(ปีที่เขียน 2014) ตามลิ้งนี้ เเล้วหาไฟล์ที่มีชื่อว่า PHPMailerAutoload.php จากนั้นในโค๊ด php เราจะ require ไฟล์นี้เข้ามาเพื่อเรียกใช้ class PHPMailer

จากนั้นเข้าบัญชี Google Account เข้าไปที่แท็ป https://www.google.com/settings/security แล้วหาบ็อก Account permissions จากนั้นคลิก enable ตั้งค่าให้ Access for less secure apps เพื่อเป็นการอนุญาติให้ app ภายนอกเข้าใช้งาน account เราได้


เราสามารถเลือก encryption system ได้ 2 แบบนั้นคือ
- ssl Port 465 (SSL required)
- tls Port 587 (TLS required)

จากนั้นตั้งค่าตัวแปรที่จำเป็นให้คลาสดังนี้
$mail->Host คือ stmp server ของกูเกิ้ลที่ใช้ส่งอีเมลล์
$mail->SMTPAuth การพิสูจน์ตัวตนด้วย username password
$mail->Username ชื่อเมลล์ที่จะล็อกอินเพื่อส่ง
$mail->Password พาส
$mail->addAddress ชื่อผู้ส่ง
$mail->Subject หัวเรื่อง
$mail->msgHTML ไฟล์ html ที่จะส่งไปผ่านเมลล์
$mail->AltBody รายละเอียดเมลล์
$mail->addAttachment ไฟล์แนบ

โค๊ดตัวอย่างดังนี้ครับ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */
 
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
 
require '../PHPMailerAutoload.php';
 
//Create a new PHPMailer instance
$mail = new PHPMailer;
 
//Tell PHPMailer to use SMTP
$mail->isSMTP();
 
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
 
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
 
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
 
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
 
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
 
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
 
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
 
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
 
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
 
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
 
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
 
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
 
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
 
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
 
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
 
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
 
 
<div class="preloader"><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
เพิ่มเติมเป็นเกร็ดความรู้ครับ เราสามารถตรวจสอบ log ของ gmail ได้ โดยจะแสดงการลงชื่อเข้าใช้แต่ละครั้งที่ตำแหน่งไหน และด้วย IP อะไร ซึ่งถ้ามีการลงชื่อจากแอพ หรือเหตุการณ์อะไรที่เป็นอันตรายกับบัญชีของเรา ก็จะแสดงให้เห็นด้วย ที่หน้าจอนี้
https://security.google.com/settings/security/activity



ไม่มีความคิดเห็น:

May be like this posts

  • [โปรแกรม searchหาชื่อสินค้า] SQLite บน C# เอาไว้อ่านไฟล์ .db ใน WinForm มันง่ายมั่กๆ
    04/09/2013 - 1 Comments
    วันก่อนผมเขียนเกี่ยวกับการสร้าง app ดึงข้อมูลจากฐานข้อมูล SQL Server 2008 r2…
  • ลิสส์รายการ Visual Studio Project Type GUIDs
    19/10/2014 - 0 Comments
    รวบรวม GUIDs ของโปรเจคค์ต่างๆบน VS ทั้งหมด เอาไว้ search หาง่ายๆหน่อย เช็คโปรเจคค์โดยการเปิดไฟล์ .sln ด้วย…
  • [WinForm c#] Basic send E-mail with Google Sevice
    29/09/2013 - 0 Comments
    In this tutorial, I gonna show you how to implement basic WinForm with sending E-mail function. Gmail…
  • XAMPP Start Apache ไม่ได้เพราะ PID 4 ไม่ว่าง แก้ยังไงมาดู
    30/04/2014 - 0 Comments
    ปัญหาเยอะจริงเวลาลงโปรแกรมใหม่ๆเข้ามา XAMPP เป็นอันต้อง port 80 ไม่ว่างบ้างกับสไกด์นี้แหละตัวดีเลย หรือ PID…
  • วิธีแก้ Couldn't Mount File
    20/04/2015 - 0 Comments
    สำหรับไฟล์ประเภท iso บน Windows 8.1 สามารถ mount หรือจำลอง disk ตัวนั้นได้เป็นไดฟ์ตัวใหม่…
  • เช็คราคาเที่ยวบินจบในคลิกเดียวด้วย Selenium IDE ใช้เวลาแค่ 3 วิ
    22/06/2017 - 0 Comments
    เวลาวางแผนไปเที่ยวต่างจังหวัดหรือต่างประเทศ มันต้องมีอารมณ์ต้องเข้าไปเช็คที่พัก…
  • ลบ .Bak แสนกวนใจใน Edit Plus
    28/05/2013 - 0 Comments
    ลบ .Bak แสนกวนใจใน Edit Plus สำหรับใครที่ใช้ Edit plus เป็น Editor ในการเขียนโค๊ดไม่ว่าจะเป็น html php c/c++…