Here most best and effective codes are available for send a mail using PHP.PHP codes are diffrent type are there for send a mail.Two steps of coding there. 1st one create a form for compose a mail.Then send the mail using while press submit button.2rd part is send mail through the server using server side script of PHP.
<?
//Send Email Code -- Begins to Users//
$to = "to@gmail.com"; // TO email id
$subject = "Subject Field";
$message = "This is example message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: <".$pemail."> \r\n";
mail($to, $subject, $message, $headers);
//Send Email Code -- End//
?>
following Example code to send an Mail using PHP..
form.php
<?php $site_admin = 'your@email.adress';
// function ae_send_mail (see code above) is pasted here
if (($_SERVER['REQUEST_METHOD'] == 'POST') &&
isset($_POST['subject']) && isset($_POST['text']) &&
isset($_POST['from1']) && isset($_POST['from2']))
{
$from = $_POST['from1'].' <'.$_POST['from2'].'>';
// nice RFC 2822 From field
ae_send_mail($from, $site_admin, $_POST['subject'], $_POST['text'],
array('X-Mailer'=>'PHP script at '.$_SERVER['HTTP_HOST']));
$mail_send = true;
}
?>
<html><head><title>Send us mail</title>
</head><body>
<?php
if (isset($mail_send)) {
echo '<h1>Form has been sent, thank you</h1>';
}
else {
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
Your Name: <input type="text" name="from1" size="30" /><br />
Your Email: <input type="text" name="from2" size="30" /><br />
Subject: <input type="text" name="subject" size="30" /><br />
Text: <br />
<textarea rows="5" cols="40" name="text"></textarea>
<input type="submit" value="send" />
</form>
<?php } ?>
</body></html>
// function ae_send_mail (see code above) is pasted here
if (($_SERVER['REQUEST_METHOD'] == 'POST') &&
isset($_POST['subject']) && isset($_POST['text']) &&
isset($_POST['from1']) && isset($_POST['from2']))
{
$from = $_POST['from1'].' <'.$_POST['from2'].'>';
// nice RFC 2822 From field
ae_send_mail($from, $site_admin, $_POST['subject'], $_POST['text'],
array('X-Mailer'=>'PHP script at '.$_SERVER['HTTP_HOST']));
$mail_send = true;
}
?>
<html><head><title>Send us mail</title>
</head><body>
<?php
if (isset($mail_send)) {
echo '<h1>Form has been sent, thank you</h1>';
}
else {
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
Your Name: <input type="text" name="from1" size="30" /><br />
Your Email: <input type="text" name="from2" size="30" /><br />
Subject: <input type="text" name="subject" size="30" /><br />
Text: <br />
<textarea rows="5" cols="40" name="text"></textarea>
<input type="submit" value="send" />
</form>
<?php } ?>
</body></html>
php function ae_send_mail.php
<?php
function ae_send_mail($from, $to, $subject, $text, $headers="")
{
if (strtolower(substr(PHP_OS, 0, 3)) === 'win')
$mail_sep = "\r\n";
else
$mail_sep = "\n";
function _rsc($s)
{
$s = str_replace("\n", '', $s);
$s = str_replace("\r", '', $s);
return $s;
}
$h = '';
if (is_array($headers))
{
foreach($headers as $k=>$v)
$h = _rsc($k).': '._rsc($v).$mail_sep;
if ($h != '') {
$h = substr($h, 0, strlen($h) - strlen($mail_sep));
$h = $mail_sep.$h;
}
}
$from = _rsc($from);
$to = _rsc($to);
$subject = _rsc($subject);
mail($to, $subject, $text, 'From: '.$from.$h);
}
?>
function ae_send_mail($from, $to, $subject, $text, $headers="")
{
if (strtolower(substr(PHP_OS, 0, 3)) === 'win')
$mail_sep = "\r\n";
else
$mail_sep = "\n";
function _rsc($s)
{
$s = str_replace("\n", '', $s);
$s = str_replace("\r", '', $s);
return $s;
}
$h = '';
if (is_array($headers))
{
foreach($headers as $k=>$v)
$h = _rsc($k).': '._rsc($v).$mail_sep;
if ($h != '') {
$h = substr($h, 0, strlen($h) - strlen($mail_sep));
$h = $mail_sep.$h;
}
}
$from = _rsc($from);
$to = _rsc($to);
$subject = _rsc($subject);
mail($to, $subject, $text, 'From: '.$from.$h);
}
?>
0 comments:
Post a Comment