Monday, February 23, 2015

Script PHP untuk Mengirim Email

PHP Script yang saya tulis kali ini adalah tentang mengirimkan email. Anda tentunya sudah tahu bahwa kita bisa mengirimkan email dengan menggunakan PHP. Sebelum saya tunjukkan script PHPnya, ada sedikit cerita kenapa saya menulis tentang script email ini.
Beberapa waktu yang lalu, saya menerima email dari salah satu pengunjung http://kursus-online.com yang menanyakan mengapa script PHP untuk mengirimkan emailnya tidak bisa digunakan untuk mengirimkan ke lebih dari 1 penerima. Padahal menurut beliau, scrip tersebut seharusnya bisa digunakan untuk mengirimkan ke 2 alamat penerima.
Nah, inilah script asli yang dikirimkannya:
?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
</div>
<div>  /****SET THE MAX CHARS FOR EACH MESSAGE***************/
 
//it is recommended not to set the max too high, to prevent extremely long messages
// from stalling your server
 
$EMAIL_MAX = 2500;
$SMS_MAX = 120;
 
/*****************************************************/
<div>  //function for stripping whitespace and some chars
function cleanUp($str_to_clean, $newlines, $spaces){
 
//build list of whitespace chars to be removed
$bad_chars = array('\r', '\t', ';');
 
//if newlines are false, add that to the list of bad chars
if(!$newlines){array_push($bad_chars, '\n');}
 
//if spaces are false, strip them too
if(!$spaces){array_push($bad_chars, ' ');}
 
$str_to_clean_a = str_replace($bad_chars, '', $str_to_clean);
$str_to_clean_b = strip_tags($str_to_clean_a);
return $str_to_clean_b;
}
 
//function to check for valid email address pattern
function checkEmail($email) {
if(!eregi("?[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
return true;
}
//function to check for valid url pattern
function checkURL($url) {
if(!eregi("?http:\/\/", $url)) {return false;}
return true;
}
?&gt;</div>
<div>
"<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</a>"&gt;
 
http://www.w3.org/1999/xhtml">
 
</div>
<div></div>
<div>  if(isset($_POST["submitForm"])){</div>
<div>  $_name = cleanUp($_POST["sender_name"], false, true);</div>
<div>  $_subject = cleanUp($_POST["sender_subject"], false, true);</div>
<div>  $_message = cleanUp($_POST["sender_message"], true, true);</div>
<div>  $_url = cleanUp($_POST["sender_url"], false, false);</div>
<div>
$_body = "You have been sent this message from your contact form\n\n";
 
if($_name){
$_body .= "NAME: $_name\n\n";
}
 
if($_email){
$_body .= "EMAIL: $_email\n\n";
}
 
if($_url){
$_body .= "URL: $_url\n\n";
}
 
if($_phone){
$_body .= "PHONE: $_phone\n\n";
}
 
if($_message){
 
//check length of body, reduce to max chars
if(strlen($_message) &gt; $EMAIL_MAX){$_message= substr($_message, 0, $EMAIL_MAX);}else{$_message = $_message;}
if(strlen($_message) &gt; $SMS_MAX){$_message2 = substr($_message, 0, $SMS_MAX);}else{$_message2 = $_message;}
}
 
</div>
<div>  //store the recipient(s)
$_to = array();</div>
<div>  //now get the recipient(s)
$_to[] = "<a href="mailto:ghilman131.ghilman@blogger.com">ghilman131 [dot] ghilman [at] blogger [dot] com</a>";
 
//define the subject
if(!$_subject){$_subject = "E-Mail from your contact form";}</div>
<div>
if(!$_name){$_name = "CONTACT FORM";}
if(!$_email){$_email = $_name;}
 
//set the headers
$_header = "From: $_name &lt; $_email &gt;" . "\r\n" .
"Reply-To: ".$_email."\r\n" .
"Super-Simple-Mailer: supersimple.org";
 
//we can send up to 2 emails (EMAIL and/or SMS)
if(count($_to) &gt; 2){ $_to = array_slice($_to,0,2);}
 
for($i=0;$i
//get the correct message, based on where it is delivering to
if(strstr($_to[$i],"teleflip.com")){$_text = $_body.$_message2;}else{$_text = $_body.$_message;}
 
//send the email(s)
mail($_to[$i], $_subject, $_text, $_header);
 
}
 
echo "";
}
?&gt;</div>
<div>

0 comments:

Post a Comment

 

© Copyright 2010 oleh HariZ| Powered By : Blogger