Experience Zone

Share Experience With Respect To FreeBSD/Debian/Ubuntu/PHP/C#

จากครั้งที่แล้วเราได้ทำการเขียนโปรแกรมส่ง email แล้วทำการตรวจสอบว่าผู้รับได้ทำการเปิด email อ่านหรือยัง คราวนี้ผมจะเพิ่มเติมโปรแกรมให้สามารถทำการตรวจสอบได้ด้วยว่า email ที่เราส่งไปหานั้นมีตัวตนอยู่จริงหรือเปล่า โดยการเพิ่ม header Return-Path เข้าไปเพื่อเป็นการบอกกับ mail server ว่าหากไม่พบ email นี้ในระบบให้ทำการ forward email ไปยัง email ที่เรากำหนดไว้ใน return-path

ซึ่งจริงๆ แล้ว Return-Path จะเป็นตัวที่จะบอกกับ email client ว่าจะให้ reply email ไปที่ไหนเมือผู้ใช้ต้องการที่จะตอบ email กลับนะครับ แต่ในกรณีนี้เราจะนำมาใช้เพื่อบอกให้ mail server ตอบเรากลับมาว่า email ที่เราส่งออกไปนั้นไม่มีผู้รับ

มาดูตัวอย่างกัน

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
<?php
 
// function ที่จะใช้ทำการส่ง email แบบ HTML
function mail_html($to,$subject,$message){
    $headers  = "Return-Path: <khwanchai@gmail.com>\n";
    //Return-Path: <khwanchai@gmail.com> ให้เปลี่ยนเป็น email ของคุณนะครับ
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: text/html; charset=tis-620\n";
    $headers .= "To: $to\n";
    $headers .= "From: khwanchai@gmail.com <khwanchai@gmail.com>\n";
 
    $message = str_replace('\"', '"', $message);
    $message = str_replace("\'", "'", $message);
 
    mail($to_none, $subject, $message, $headers);
}
 
 
$message = '
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
';
 
$subject = "Test Return-Path";
 
$to1  = "xxx1234yyy@hotmail.com";
$to2  = "xxx1234yyy@yahoo.com";
$to3  = "xxx1234yyy@gmail.com";
 
 
mail_html($to1,$subject,$message);
mail_html($to2,$subject,$message);
mail_html($to3,$subject,$message);
 
print "OK";
?>

จากตัวอย่างจะทำการส่ง email ไปหา email ที่ไม่มีอยู่จริงใน hotmail,yahoo และ gmail ซึ่งเมื่อทำการรันโปรแกรมแล้ว ให้รอสักครู ไม่เกิน 5 นาที ก็จะมี email ตอบกลับจาก server ว่าไม่พบ email ที่เราส่งไปหา โดยจะมีเนื้อหาใน email ที่ตอบกลับมา ดังนี้
(ในแต่ละ server อาจจะไม่เหมือนกันนะครับ)

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
Hi. This is the qmail-send program at myserver.com.
I'm afraid I wasn't able to deliver your message to the following addresses.
This is a permanent error; I've given up. Sorry it didn't work out.
 
<xxx1234yyy@hotmail.com>:
65.54.253.230 does not like recipient.
Remote host said: 550 Requested action not taken: mailbox unavailable
Giving up on 65.54.253.230.
 
--- Below this line is a copy of the message.
 
Return-Path: <khwanchai@gmail.com>
Received: (qmail 77552 invoked by uid 2527); 19 Jun 2005 12:21:22 -0000
Date: 19 Jun 2005 12:21:22 -0000
Message-ID: <20050619122122.77551.qmail@myserver.com>
To:
Subject: Test Return-Path
MIME-Version: 1.0
Content-Type: text/html; charset=tis-620
To: xxx1234yyy@hotmail.com
From: khwanchai@gmail.com <khwanchai@gmail.com>
 
 
 
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>
<b>Test Return-Path</b><br>

เพิ่มเติม
จากการทดลองส่งไปยัง mail server ต่างๆ ก็เห็นว่าโดยส่วนใหญ่แล้วสามารถตอบกลับมาได้ ว่า email ที่เราส่งออกไปนั้นไม่มีอยู่จริง
ยกเว้น mail server บางที่ที่เขาทำการปิดบริการตรงนี้เอาไว้ อาจจะด้วยเหตุผลด้านสุขภาพมังผมไม่แน่ใจ

Add A Comment


 Enter this code