NameSilo

Mail()

SpaceshipSpaceship
SpaceshipSpaceship
NamecheapNamecheap
Watch
I think is subforum is a great idea, so i decided to do something in it. A famous function, yet mis-used is mail().

Mail function can be used in several ways, eg a contact form and auto-senders suitable for confirming registeration.

Ok the function is used like this:

PHP:
mail($to, $subject, $message, $headers);
you can either set the varaibles or enter them manually like this:

PHP:
mail([email protected], message subject, message , headers);
Although its prefered to use variables becuae its easier to use and modify later.

To make sure the message was sent correctly you can use the following "if block"
PHP:
if(mail($to,$subject,$message,$headers)){
echo "Email Sent";
}else{
echo "Email Sending Failed";
}
If you are making an autosender you add this to the page that has the email address passed in most propably by a form, so you do this:
PHP:
$to="[email protected]";
$from=$_POST['email'];
$message="my message";
$subject="Subject of message";
$headers = "From: $from";
if(mail($to,$subject,$message,$headers)){
echo "Email Sent messaage";
}else{
echo "Email Sending Failed mesasage";
}
The other emaple i mentioned at the begginig was the contact form, look at this form, it has both parts in one php page:
PHP:
<?php
if(!isset($submit)){
echo "<form action='$_SERVER[PHP_SELF]' method='post'>
<p>Name: <input type='text' name='name'><br><p>
<p>Email: <input type='text' name='email'><br><p>
Message: <br>
<textarea name='message' cols='40' rows='15'></textarea><br>
<input type='submit' name='submit' value='send'>
</form>";
}else{
$to="[email protected]";
$from=$_POST['email'];
$message=$_POST['message'];
$subject="Website Contact Form";
$submit=$_POST['submit'];
$headers = "From: $from";
if(mail($to,$subject,$message,$headers)){
echo "Email Sent";
}else{
echo "Email Sending Failed";
}
}
?>
an example of it is available here .

I hope this is helpful and makes the mail() function clear and easier to use.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back