E-mailov² klient pre webmail v PHP - adresßr a doplnkovΘ funkcie
V tomto zßvereΦnom Φlßnku o tvorbe webmailu vßs oboznßmim s doplnkov²mi funkciami, vymazßvanφm e-mailov, Φφtanφm odoslan²ch e-mailov a adresßrom.
PoslednΘ s·bory tohto webmailu s· nasledovnΘ:
- delete.php - s·bor pre vymazßvanie e-mailov
- delete_address.php - vymazßvanie adries z adresßra
- insert_address.php - vkladanie adries do adresßra
- read_sent.php - Φφtanie odoslan²ch sprßv
- select_address.php - v²ber viacer²ch adries prijφmate╛ov e-mailu
- send_to_address.php - tieto adresy upravφ a vlo₧φ do formulßra na pφsanie sprßv (new.php)
- sent.php - odoslanΘ sprßvy
Vymazßvanie e-mailov (delete.php)
ZaΦneme od prvΘho s·boru delete.php, ktor² vymazßva e-mailovΘ sprßvy. Zdrojov² k≤d s·boru a funkcie:
<?php
session_start();
require "./functions.php";
html_header();
delete_message($id);
html_footer();
?>
// functions.php
<?php
function delete_message($id)
{
global $userid;
$imap = open();
if(!$imap) return false;
$num_id = count($id);
if($num_id>0):
for($i=0;$i<$num_id;$i++):
imap_delete($imap,$id[$i]);
endfor;
imap_expunge($imap);
if($num_id==1) echo "<div align='center' class='middle'>Sprßva bola vymazanß.</div>";
elseif($num_id>1) echo "<div align='center' class='middle'>Sprßvy boli vymazanΘ.</div>";
imap_close($imap);
else:
echo "<div align='center' class='warning'>Äiadna sprßva nebola vybranß k vymazaniu.</div>";
endif;
}
?>
Mazanie e-mailov²ch sprßv prebieha vo funkcii delete_message() na zßklade identifikaΦnΘho Φφsla sprßvy. Vymazßva¥ sa dß viac sprßv zßrove≥, premennß $id je teda pole. Vstup je z v²beru sprßv, kde oznaΦφme tie, ktorΘ chceme vymaza¥ a formulßr odoÜleme. Vo funkcii m⌠₧ete nßjs¥ tieto IMAP funkcie:
- int imap_delete(int spojenie, int cislo_spravy); - funkcia oznaΦφ sprßvu pre zmazanie
- int imap_expunge(int spojenie); - funkcia vyma₧e sprßvy oznaΦenΘ funkciou imap_delete()
- int imap_close(int spojenie, int moznosti); - uzatvorφ spojenie
Adresßr
Adresßr je u₧itoΦnß vec, ktorß nebude ch²ba¥ ani v naÜom webmaile. Ten nßÜ bude schopn² pridßva¥, vymazßva¥ a hlavne zobrazova¥ a prakticky vyu₧φva¥ kontakty. ZaΦneme pekne od zaΦiatku - pridßvanie (insert_address.php):
<?php
session_start();
require "./functions.php";
html_header();
insert_address_form($_GET);
html_footer();
?>
// functions.php
function insert_address_form($form)
{
global $userid;
$form['name'] = trim($form['name']);
$form['mail'] = trim($form['mail']);
if(!isset($form['sent'])):
?>
<form action="insert_address.php" method="get">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><div align="left" class="middle">Meno </div></td>
<td><input type="text" class="textinput" name="name" /></td>
</tr>
<tr>
<td><div align="left" class="middle">Adresa </div></td>
<td><input type="text" class="textinput" name="mail" value="<?php echo form['mail']?$form['mail']:""?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><button type="submit" class="button">Vlo₧i¥</button></td>
</tr>
</table>
<input type="hidden" name="sent" value="1" />
</form>
<?php
else:
db_connect();
$query = "insert into address values('','$userid','$form[name]','$form[mail]')";
$result = mysql_query($query);
if(!$result)
{
echo "<div class='warning' align='center'>Kontakt sa nepodarilo vlo₧i¥.</div>";
return;
}
echo "<div class='warning' align='center'>Kontakt bol ·speÜne pridan².</div>";
endif;
}
Jednoduchß funkcia, ktorß umo₧≥uje vklada¥ novΘ kontakty. Pokia╛ je formulßr odoslan², tak sa ·daje spracuj· - vlo₧ia do databßze. V opaΦnom prφpade sa zobrazφ formulßr na vkladanie nov²ch kontaktov.
Vymazßvanie kontaktov bude prebieha¥ v s·bore delete_address.php:
<?php
session_start();
require "./functions.php";
html_header();
delete_address($_GET);
html_footer();
?>
// functions.php
function delete_address($form)
{
global $userid;
$address = trim($form['address']);
if(empty($address)):
db_connect();
$query = "select * from address where userid = '$userid'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0):
?>
<select name='address' class='select'>
<option value='false'>--vyber--</option>
<?php
for($i=0;$i<$num_result;$i++):
$name = mysql_result($result,$i,"name");
$address = mysql_result($result,$i,"mail");
echo "<option value='$address'>$name <".$address."></option>";
endfor;
?>
</select>
<button class="button" type="submit">Vymaza¥</button>
<?php
else:
echo "<div class='warning' align='center'>V adresßri nemßte ₧iadne zßznamy.</div>";
endif;
else:
db_connect();
$query = "delete from address where userid = '$userid' and address = '$address'";
$result = mysql_query($query);
if(!$result)
{
echo "<div class='warning' align='center'>Zßznam sa nepodarilo vymaza¥.</div>";
return;
}
else echo "<div class='middle' align='center'>Zßznam bol ·speÜne vymazan².</div>";
endif;
}
Vymazßvanie prebieha tie₧ ve╛mi jednoducho - kontakt, ktor² chceme vymaza¥, vyberieme z formulßrovΘho prvku select a formulßr odoÜleme. Funkcia ako v predoÜlom prφpade aj v tomto funguje na princφpe, ₧e ak sme nieΦo posielali, tak sa spracuje, pokia╛ nie, tak sa zobrazφ formulßr.
Adresßr sl·₧i hlavne k v²beru adries prijφmate╛ov sprßvy. To sa odohrßva v s·bore select_address.php:
<?php
session_start();
require "./functions.php";
html_header();
select_address($_GET);
html_footer();
?>
// functions.php
function select_address($form)
{
global $userid;
$address = trim($form['address']);
if(empty($address)):
db_connect();
$query = "select * from address where userid = '$userid'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0):
?>
<form action='send_to_address.php' method='post'>
<div class='middle'>
<?php
for($i=0;$i<$num_result;$i++):
$name = mysql_result($result,$i,"name");
$address = mysql_result($result,$i,"mail");
echo "<input type='checkbox' name='adresy[]' value='$address' />$name <".$address."> <br />";
endfor;
?>
</div>
<button class="button" type="submit">Posla¥</button>
</form>
<?php
else:
echo "<div class='warning' align='center'>V adresßri nemßte ₧iadne zßznamy.</div>";
endif;
else:
db_connect();
$query = "delete from address where userid = '$userid' and address = '$address'";
$result = mysql_query($query);
if(!$result)
{
echo "<div class='warning' align='center'>Zßznam sa nepodarilo vymaza¥.</div>";
return;
}
else echo "<div class='middle' align='center'>Zßznam bol ·speÜne vymazan².</div>";
endif;
}
V²ber adries sa realizuje zaÜkrtnutφm prvku checkbox. Ten mß nßzov adresy[], Φφm docielime to, aby na v²stupe bolo pole, ktorΘ bude obsahova¥ adresy prijφmate╛ov.
Posledn² s·bor t²kaj·ci sa adresßru je send_to_address.php, ktor² je vizußlne toto₧n² s new.php, kde sa pφÜe novß sprßva. Tento s·bor len vytvßra adresu odosielate╛a z v²beru adries z predoÜlej strßnky select_address.php.
<?php
session_start();
require "./functions.php";
html_header();
$to = get_address($_POST['adresy']);
show_form($to);
html_footer();
?>
// functions.php
function get_address($adresy)
{
$count = count($adresy);
if($count==0) return '';
for($i=0;$i<$count;$i++):
if($i==($count-1)) $to .= $adresy[$i];
else $to .= $adresy[$i].",";
endfor;
return $to;
}
Funkcia get_address e-mailovΘ adresy zoradφ za sebou, oddelφ Φiarkou a ulo₧φ do premennej $to. T·to potom predß funkciφ show_form(), ktor· sme si popφsali u₧ v prvom Φlßnku.
╚φtanie odoslan²ch mailov (sent.php)
Webmail nßm dovolφ si Φφta¥ aj odoslanΘ e-mailovΘ sprßvy. Tieto sa bud· uklada¥ do databßzy. K≤d s·boru sent.php k zobrazovaniu a Φφtaniu odoslan²ch mailov vyzerß nasledovne:
<?php
session_start();
require "./functions.php";
html_header();
list_sent($_GET);
html_footer();
?>
// function.php - funkcia list_sent();
function list_sent($form)
{
global $userid;
if(empty($form['orderby'])) $form['orderby'] = "date";
db_connect();
$query = "select * from sent where userid = '$userid' order by $form[orderby] desc";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0):
echo "<table cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td width='150'><div class='middle' align='center'><a href='sent.php?orderby=to' title='attachment'>Prφjmate╛</a></div></td>";
echo "<td width='200'><div class='middle' align='center'><a href='sent.php?orderby=subject' title='attachment'>Predmet</a></div></td>";
echo "<td width='100'><div class='middle' align='center'><a href='sent.php?orderby=date' title='attachment'>OdoslanΘ</a></div></td>";
echo "</tr>";
for($i=0;$i<$num_result;$i++):
$id = mysql_result($result,$i,"id");
$to = mysql_result($result,$i,"to_address");
$subject = mysql_result($result,$i,"subject");
$date = mysql_result($result,$i,"date");
$date = date("j.m.Y H:i:s",$date);
if($i%2==0) echo "<tr bgcolor='#CCCCCC'>";
else echo "<tr bgcolor='#DDDDDD'>";
echo "<td><div class='middle' align='left'><a href='read_sent.php?id=$id'> $to</a></div></td>";
echo "<td><div class='middle' align='left'><a href='read_sent.php?id=$id'> $subject</a></div></td>";
echo "<td><div class='middle' align='left'><a href='read_sent.php?id=$id'> $date</a></div></td>";
echo "</tr>";
endfor;
echo "</table>";
else:
echo "<div class='warning' align='center'>V odoslanej poÜte nemßte ₧iadne sprßvy.</div>";
endif;
}
// read_sent.php - Φφtanie odoslanΘho mailu
<?php
session_start();
require "./functions.php";
html_header();
show_sent_message($id);
html_footer();
?>
// function.php - funkcia show_sent_message();
function show_sent_message($id)
{
global $userid;
if(isset($id)):
db_connect();
$query = "select * from sent where userid = '$userid' && id = '$id'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0):
$id = mysql_result($result,0,"id");
$to = mysql_result($result,0,"to_address");
$subject = mysql_result($result,0,"subject");
$message = mysql_result($result,0,"message");
$date = mysql_result($result,0,"date");
$date = date("j.m.Y H:i:s",$date);
// zobrazφme sprßvu
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td width="25%"><div class="middle" align="left">Prφjmate╛:</div></td>
<td width="25%"><div class="middle" align="left"><?php echo $to?></div></td>
<td></td>
</tr>
<tr>
<td><div class="middle" align="left">K≤pia:</div></td>
<td><div class="middle" align="left"><?php echo $cc?></div></td>
<td></td>
</tr>
<tr>
<td><div class="middle" align="left">Predmet:</div></td>
<td><div class="middle" align="left"><?php echo $subject?></div></td>
<td></td>
</tr>
<tr>
<td><div class="middle" align="left">Dßtum:</div></td>
<td><div class="middle" align="left"><?php echo $date?></div></td>
<td></td>
</tr>
<tr><td height="15"></td></tr>
<tr><td colspan="3">
<div class="text">
<?php
echo nl2br($message);
?>
</div>
</td></tr>
</table>
<?php
else:
echo "<div class='warning' align='center'>Sprßva nebola nßjdenß.</div>";
endif;
else:
echo "<div class='warning' align='center'>Sprßva nebola nßjdenß.</div>";
endif;
}
DoplnkovΘ funkcie
Na zßver eÜte nesm· ch²ba¥ spomφnanΘ doplnkovΘ funkcie:
{
global $userid;
db_connect();
$query = "select * from accounts where userid = '$userid'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0) $address = mysql_result($result,0,"address");
return $address;
}
function name()
{
global $userid;
db_connect();
$query = "select * from accounts where userid = '$userid'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
if($num_result>0) $name = mysql_result($result,0,"name");
return $name;
}
Funkcia address() vyberie z databßzy vaÜu e-mailov· adresu, ktorß je potrebnß pri odosielanφ e-mailov. Funkcia name() vyberß z databßze meno, ktorΘ sa zobrazφ prijφmate╛ovi sprßvy.
K dispozφcii s· vßm zdrojovΘ s·bory celΘho webmailu. M⌠₧ete k nemu jednoducho prirobi¥ prihlasovacφ systΘm s registrßciou, kde budete m⌠c¥ vytvori¥ viacero e-mailov²ch k⌠nt pre viacer²ch u₧φvate╛ov.
E-mailov² klient pre webmail v PHP
Jednoduch² web-based klient pro e-mail ukazuje principy vyu₧itφ IMAP funkcφ v PHP k p°φstupu a prßci s e-mailov²mi zprßvami. SouΦßstφ serißlu jsou kompletnφ zdrojovΘ k≤dy - klient je po sta₧enφ p°ipraven k okam₧itΘmu pou₧itφ. Tato sΘrie Φlßnk∙ ji₧ byla uzav°ena, aΦkoli dalÜφ pokraΦovßnφ nelze vylouΦit.
- E-mailov² klient pre webmail v PHP - adresßr a doplnkovΘ funkcie (prßv∞ Φtete)
- E-mailov² klient pre webmail v PHP - odosielanie, Φφtanie a s¥ahovanie
- E-mailov² klient pre webmail v PHP - zßkladnß Ütrukt·ra