Monday 27 August 2012

How To Send Mail In PHP

PHP code can send email messages without the need for site administrators to handle them manually. When Web developers build sites and applications, they can opt to send email messages as part of this processing. If you have ever signed up to a website or online service, you may have received an email confirming your account details. These emails are often not sent manually by a person using a standard email client, but by automated scripts. In PHP scripts, you can use sendmail functions to implement this process, which is common in server side scripting.

Purpose

The purpose of sendmail scripts in a PHP site is to send email messages. The sendmail functions allow developers to build email messages in HTML or plain text, then to send these messages to specified recipients.

PHP scripts can set various properties of a message before sending it, including headers, titles, lists of recipients to copy to and the content itself. Often, developers define individual mail scripts dedicated to sending standard email messages for set processes, such as confirming user accounts.

When a PHP script actually sends a message, it can use the mail function. If the recipient chooses to reply to the message, their reply will be sent to the specified "from" address in the script. Using the headers section of the function, developers can include multiple aspects of the message, including MIME and content type.

Syntax

When developers implement sendmail scripts, they use the mail function, which offers a range of options. The following sample code demonstrates calling the function inside a PHP script:

<?php
$mail_to = "person@somedomain.com";
$mail_subj = "Hello";
$mail_msg = "

This is the content of the email message

"; $mail_frm = "sender@somedomain.com"; $mail_head = "MIME-Version: 1.0\r\n"; $mail_head .= "Content-type: text/html; charset=iso-8859-1"; mail($mail_to, $mail_subj, $mail_msg, $mail_head); ?>

The mail function can receive four parameters, representing the email address to send to, the subject of the message, the message body and any headers the script is setting. This script builds each of the component parts as individual parameters, then passes them to the function when it executes.

Uses

Sites use PHP mail functions for various different purposes. Managing user accounts is a common activity in which sendmail scripts tend to feature, not only for signing users up with new accounts but for account administration actions such as changing passwords and informing users of changes to accounts. Commercial websites also use server side scripts to send automated emails to customers, for example to confirm purchases, payments and dispatching of products.

Considerations

The PHP sendmail functions provide a degree of flexibility for developers. For example, scripts can send HTML formatted mail rather than plain text. However, developers need to consider the email clients and other environmental factors for their users. For example, some HTML email content may not be visible to users on certain platforms. Developers also need to bear in mind the fact that users may be receiving their messages on mobile devices with restricted display and connectivity. For this reason, some developers keep the formatting and content of automated emails as simple as possible.

See Also:
How To: AJAX PHP Functions
How To: JavaScript PHP Function Calls

No comments:

Post a Comment