php භාෂාව භාවිතා කරමින් විද්‍යුත් ලිපියක් (email) යැවීම සදහා කේත ලියන ආකාරය

වෙබ් අඩවි සංවර්ධනයේදී අපට සිදුවනවා යමෙකුට විද්‍යුත් ලිපි (e mail) යැවීම සදහා යෙදවුම් නිර්මානය කිරීමට. මා පහත දක්ව ඇත්තේ යන php භාෂාව මගින් විද්‍යුත් ලිපි යැවීම සදහා සකසන ලද කේතයකි.

පලමුව මේ සදහා පහත ආකාරයේ ෆොරමයක් සාදා ගත යුතුව ඇත. එය මගින් සකසා ගත යුතුය.එහි කේත පහත පරිදි වෙයි

index.html ගොනුව


<html>
<head>
<title>E mail Form</title>
</head>
<body>
<form name="contactform" method="post" action="email.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input  type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input  type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input  type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input  type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

පහත ඇති කේතය මගින්, ෆෝරමයෙන් ලැබේන දත්ත සකසා එම දත්ත විද්‍යුත් ලිපියක් ලෙස යවනු ලබයි.එය පහත පරිදි නම් කිරීම සිදු කරන්න.

email.phpගොනුව


<?php
if(isset($_POST['email'])) {
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
echo "ERROR..! there were error(s) found with the form submision. ";
echo "These errors detected.<br /><br />";
echo $error."<br /><br />";
echo "Please fix these errors before submit.<br /><br />";
die();
}
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('Submission ERROR.!');
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered invalid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered ivalid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered ivalid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered ivalid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form submission details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<p>Thank you for contacting us. We will be in touch with you very soon.</p>
<?php
}
?>

ඔබ මෙය ඔබගේ පුද්ගලික පරිඝනකයේ නම් පරික්ෂාකර බලන්නේ (local testing), ඔබට ඔබගේ php ස්තාපනයෙදි SMTP සර්වරය ද සැකසිය යුතු වෙයි.නොමැති නම් ඔබට මෙහි ප්‍රතිපලය නොලැබී පහත පණිවිඩය ලැබෙනු ඇත

Warning: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()

SMTP සර්වරය සකසා ගන්නා ආකාරය සදහා මෙම ලිපියට යොමුවන්න

About Umanda Jayobandara (උමන්ද ජයෝබණ්ඩාර)

I am a Software Engineer in Sri Lanaka. Please visit my web site for more info http://umandajayobandara.com/
This entry was posted in කේත උදාහරන, php කේත උදාහරන. Bookmark the permalink.

10 Responses to php භාෂාව භාවිතා කරමින් විද්‍යුත් ලිපියක් (email) යැවීම සදහා කේත ලියන ආකාරය

  1. Aravinda Godakawela පවසයි:

    Html ekai php ekai join karanne kohomada dekama index.html ekata copy past kaloth harida

    කැමතියි

  2. rehan පවසයි:

    කෝ smtp එක හදන ලිපිය?

    කැමතියි

  3. ano පවසයි:

    SMTP සර්වරය සකසා ගන්නා ආකාරය සදහා මෙම ලිපියට යොමුවන්න

    කොයි ලිපියද දන්නේ නෑ?

    කැමතියි

  4. sameera පවසයි:

    mekanam niyamai… mama hoya hoya hitiye….. but podi error ekak awa

    කැමතියි

  5. ~ ලකී ~ පවසයි:

    ගොඩාක් නොමිලේ සපයන වෙබ් හෝස්ටින් වල PHP Sendmail disable කරල නේ….

    කැමතියි

ලිපිය සම්බන්දව ඔබගේ අදහස් ලබා දෙන්න