ถ้าระบบเรามีการสมัครสมาชิก สมาชิกถูกแบ่งออกเป็นกลุ่มๆ
แล้วเราจะเขียนโปรแกรมให้ส่งเมลล์ไปหาสมาชิกแบบเลือกเป็นรายคน เป็นกลุ่มๆ
หรือเลือกทั้งหมด โดยเลือกจากฐานข้อมูลที่มีอยู่แล้ว
จะมีวิธีการเขียนยังไงค่ะ
ขอบคุณมากค่ะ
ปกติแล้วรูปแบบ code email ไม่มีอะไรยุ่งยากครับ ผมแนะนำให้ใส่ loop อาจจะเขียน
if else ปกติดก็สามารถวนได้แล้ว ส่วนการเลือกกลุ่ม ก็น่าจะมีการแบ่งประเภท member ไว้ได้ ก็เลือกจา กลุ่มมาส่ง ลองทำดูนะครับ :D
<?
$strTo = "member@hostdrift.com";
$strSubject = "หัวข้าเมล";
$strHeader = "From: admin@hostdrift.com";
$strMessage = "ข้อความใน email";
$flgSend = @mail($strTo,$strSubject,$strMessage,$strHeader); // ใส่ @ = ไม่แสดง errorไว้ //
if($flgSend)
{
echo "ส่งอีเมลเรียบร้อยแล้ว.";
}
else
{
echo "ไม่สามารถส่ง email ได้.";
}
?>
ปัญหาไม่ได้อยู่ที่การส่ง เพราะจะส่งคนเดียวหรือหลายคนก็วิธีเกียวกันครับ
หลัการจะไปอยู่ที่การ query ครับ ซึ่งก็แล้วแต่ฐานข้อมูลเช่น
กำหนดให้ group แทนกลุ่ม เก็บข้อมูลเป็นตัวเลข เช่น กลุ่ม 1, กลุ่ม 2
SELECT * FROM member_table WHERE group=1
ข้อมูลที่ได้คือ รายการสมาชิกในกลุ่ม เราก็วนลูปเิอารายชื่อนี้ไปส่งอีเมล์ทีละคน ครับ
ขอบคุณมากค่ะ
เดี๋ยวจะลองทำดูค่ะ
ทำได้แล้วค่ะ ^^
ไม่ทราบว่าทำยังไงหรอค่ะ มีปัญหาอยู่เหมือนกัน
ขอโค้ดหน่อยน่ะค่ะ
ส่งเมลล์แบบกลุ่มได้แล้วค่ะ
ทีนี้อยากจะใช้ฟังก์ชั่น PHPMailer ในการส่งเมลล์ทั้งหมด
และแนบไฟล์ด้วย ไม่รู้ว่าผิดตรงไหน
ช่วยดูให้หน่อยค่ะ
ขอบคุณมากล่วงหน้านะค่ะ
<? session_start();?>
<?
include ('../../config/conn.inc.php');
include("../../config/function.php");
chk_login("Administrator");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Send Mail</title>
</head>
<body>
<?php
if($_POST) {
set_time_limit(0); //กำหนดเป็น 0 คือไม่จำกัดเวลาในการส่ง
include "../../PHPMailer/class.phpmailer.php";
$mailer = new PHPMailer();
$ref_tg_id = $_POST['to'];
$sql = "SELECT * FROM company where ref_tg_id = '$ref_tg_id';";
$result = mysql_query($sql);
//ป้องกันการใส่ backslash เครื่องหมาย quote
if(get_magic_quotes_gpc()) {
foreach($_POST as $key => $value) {
$_POST[$key] = stripslashes($value);
}
}
$mailer->WordWrap = 70;
$mailer->CharSet = "utf-8";
$mailer->IsHTML(true);
$mailer->From = mail@hotmail.com;
$mailer->FromName = iconv("tis-620", "utf-8", $_POST['fromname']);
//อัปโหลดไฟล์
if($_FILES) {
@mkdir("upload");
$path = "upload/" . $_FILES['upfile']['name'];
move_uploaded_file($_FILES['upfile']['tmp_name'], $path);
$mailer->AddAttachment($path);
}
$mailer->Subject = iconv("tis-620", "utf-8", $_POST['subject']);
$mailer->Body = iconv("tis-620", "utf-8", $_POST['body']);
while($data = mysql_fetch_array($result)) {
$to = $data['company_mail'];
$mailer->AddAddress=$to;
$mailer->Send();
}
echo "เมลถูกส่งไปยังปลายทางแล้ว";
exit;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="549" border="0" align="center" cellpadding="0" cellspacing="3">
<tr>
<td width="89">To :</td>
<td width="451">
<select name="to" id="to" >
<option value="" >--Select--</option>
<?php
$sql2 = "select * from type_group";
$query2 = mysql_query($sql2)or die("ERROR=$sql2");
$num2 = mysql_num_rows($query2);
for($i=1;$i<=$num2;$i++)
{
$row2 =mysql_fetch_array($query2);
?>
<option value="<? echo $row2['tg_id']?>"><? echo $row2['tg_name'];?></option>
<?
}
?>
</select>
</td>
</tr>
<tr valign="top">
<td>Subject :</td>
<td><label>
<input name="subject" type="text" id="subject" size="50" />
</label></td>
</tr>
<tr valign="top">
<td>message :</td>
<td><label>
<textarea name="body" cols="50" rows="5" id="body"></textarea>
</label><label></label></td>
</tr>
<tr>
<td>File :</td>
<td><input name="upfile" type="file" id="upfile" size="35" /></td>
</tr>
<tr>
<td>Name :</td>
<td><label>
<input name="fromname" type="text" id="fromname" size="50" />
</label></td>
</tr>
<tr>
<td> </td>
<td align="reft"><label>
<input type="submit" name="Submit" value="Send Mail" />
</label></td>
</tr>
</table>
</form>
</body>
</html>