PHP Sending E-mailsPHP allows you to send e-mails directly from a script. One of the major uses of a server side scripting language is to provide a way of sending e-mail from the server and, in particular, to take form input and output it to an e-mail address. In this part I will show you how to send e-mail messages using PHP. Mail is extremely easy to send from PHP, unlike using scripting languages which require special setup (like CGI). There is actually just one command, mail() for sending mail. The PHP mail() function is used to send emails from inside a script. Syntax: mail(to,subject,message,headers,parameters) | Parameter | Description | | to | Required. Specifies the receiver / receivers of the email | | subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters | | message | Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters | | headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) | | parameters | Optional. Specifies an additional parameter to the sendmail program |
Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. The simplest way to send an email with PHP is to send a text email. In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail: Code: <?php $to = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
"; $headers = "From: $from"; mail($to,$subject,$message,$headers); echo "Mail Sent."; ?> With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address: Code: <html> <body> <?php if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
", "Subject: $subject", $message, "From: $email" ); echo "Thank you for using our mail form"; } else //if "email" is not filled out, display the form { echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text' /><br /> Subject: <input name='subject' type='text' /><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type='submit' /> </form>"; } ?> </body> </html> This is how the example above works: - First, check if the email input field is filled out
- If it is not set (like when the page is first visited); output the HTML form
- If it is set (after the form is filled out); send the email from the form
- When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email
Note: This is the simplest way to send e-mail, but it is not secure. PHP mail() Function Requirements For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. The mail functions are part of the PHP core. There is no installation needed to use these functions. The behavior of the mail functions is affected by settings in the php.ini file. Mail configuration options: | Name | Default | Description | Changeable | | SMTP | "localhost" | Windows only: The DNS name or IP address of the SMTP server | PHP_INI_ALL | | smtp_port | "25" | Windows only: The SMTP port number. Available since PHP 4.3 | PHP_INI_ALL | | sendmail_from | NULL | Windows only: Specifies the "from" address to be used in email sent from PHP | PHP_INI_ALL | | sendmail_path | NULL | Unix systems only: Specifies where the sendmail program can be found (usually /usr/sbin/sendmail or /usr/lib/sendmail) | PHP_INI_SYSTEM | PHP: indicates the earliest version of PHP that supports the function. | Function | Description | PHP | | ezmlm_hash() | Calculates the hash value needed by the EZMLM mailing list system | 3 | | mail() | Allows you to send emails directly from a script | 3 | None.
|