Monday, February 16, 2009

Mail Function in PHP

The PHP mail() function takes up to five arguments, all of them strings, as follows:

1. The address(es) of the recipient(s)
2. The subject line
3. The message body
4. A list of other email headers
5. Additional parameters

1. The first three arguments are required. Email addresses in the first argument can be in
either of the following formats:

'user@example.com'
'Some Guy '

To send to more than one address, use a comma-separated string like this:
'user@example.com, another@example.com, Some Guy '

2. The second argument is a string containing the subject line.

3. The third argument is the message body, which must be presented as a single
string, regardless of how long it is.

4. Most people are unlikely to need the fifth argument, although some hosting
companies now make it a requirement. It ensures that the email is sent by a
trusted user, and it normally consists of -f followed (without a space) by your
own email address, all enclosed in quotes. Check your hosting company’s instructions to see whether this is required and the exact format it should take.

The importance of mail() Function:

program. It passes the address, subject line, message, and any additional email headers to the web server’s mail transport agent (MTA). PHP’s responsibility ends there. It has no way of knowing if the email is delivered to its intended destination.
Note: Email doesn’t always arrive when testing mail() in a local testing environment.
Normally, this has nothing to do with your configuration, but with your service
provider’s security policies. If email fails to arrive, upload the script to your remote server and test it there.

Removing unwanted backslashes from form input:

Eliminating magic quotes

Make sure magic quotes function is not disables.If your magic quotes is off then our single quotes and special characters may provide an error

Like '(apostaphis), spaces etc

Even if your Magic Quotes are off. You can enable them by using the following function.

if (function_exists('nukeMagicQuotes')) {
nukeMagicQuotes();
}
?

Code for sending mail is:

if (array_key_exists('send', $_POST)) {
$to = 'me@example.com'; // use your own email address
$subject = 'Feedback from Japan Journey site';
// process the $_POST variables
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// send it
$mailSent = mail($to, $subject, $message);
}

Note:
1. This entire section of code is wrapped in an if statement, which uses the function
array_key_exists(). here send name attribute of the form’s submit button

2. array_key_exists() is a foolproof way of checking that a form has been submitted.When the page first loads, there’s no way that the submit button can have been clicked, so its name isn’t present in the $_POST array. As a result, array_key_exists('send', $_POST) equates to false, and everything inside the if statement is ignored. However, as soon as the button is clicked, the page reloads, array_key_exists('send',$_POST) equates to true, and the email script is processed

Description of the above code:

The code that does the processing consists of five stages. The first two lines assign
your email address to $to and the subject line of the email to $subject. The next section labeled “process the $_POST variables” reassigns $_POST['name'], _POST['email'], and $_POST['comments'] to ordinary variables. This makes them easier to handle when you subject them to security checks or style the email later. Next, you build the body of the email message, which must consist of a single string. By using double quotes, you can embed the variables in the string and use \n to insert new line characters. Once the message body is complete, it’s passed to the wordwrap() function,which takes two arguments: a string and an integer that sets the maximum length of each line. Although most mail systems will accept longer lines, it’s recommended to limit each line to70 characters.

After the message has been built and formatted, the recipient’s address, the subject
line, and the body of the message are passed to the mail() function. The function
returns a Boolean value indicating whether it succeeded in passing the email to the
MTA. So, it’s useful to capture that value as $mailSent. You can then use $mailSent
to redirect the user to another page or change the contents of the current one.

Simply you can include the code after your mail() function

<?php
if ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message.
Please try later.<?php
}
elseif ($_POST && $mailSent) {
?>

An important note before sending a mail is check whether the required fields are empty or not.

// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
// build the message
<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>

0 comments:

Post a Comment