Overview
Using the PHP programming language, you are able to create simple mail forms to send email. PHP also has a set of more advanced IMAP functions you can take advantage of to create more advanced email forms. This allows you to save sent emails and check your inbox for new messages.
Creating custom forms is beyond the scope of technical support. If anything does not work as intended, DreamHost support is unable to troubleshoot issues related to the coding of the form.
General notes on website forms
Examples on this page show a message sent through the DreamHost web server for the 'From' email address listed. Because of this, the message is subject to the sender’s domain policy:
If you need to use an outside email address for the ‘From’ sending user, you may want to consider setting up an SMTP form for the site instead.
Please note that there are strict sending limits when sending emails from a web server. Visit the following article for further details:
Basic mail() function replacement
The imap_mail() function can replace the standard mail() function typically used for sending email. Parameters for both functions are mostly similar.
Here's a basic example using imap_mail():
<?php $to = "recipient@example.com, recipient2@example.com"; $subject = "Test Email"; $body = "This is only a test."; $headers = "From: me@example.com\r\n". "Reply-To: me@example.com\r\n"; $cc = null; $bcc = null; $return_path = "me@example.com"; imap_mail($to, $subject, $body, $headers, $cc, $bcc, $return_path); ?>
Explanation of fields on this form
- $headers
- This section has two fields, From and Reply-To:
- The From email address should be set with the email address sending the message. If you’re not using SMTP authentication, this must be an email address on the same domain you’re sending from and must be hosted with DreamHost. If you want to use any other address as the ‘From’, you must use SMTP authentication.
- The Reply-To email address can be set with any address and receives the replies back to the message from the recipient.
- $return_path
- Rejected mail goes back to the envelope sender listed here. You want the Reply-To and Return-Path set to the same sending user.
Saving sent emails
The form setup above allows you to send the message but does not save it in the SENT folder. If you want to see a record of all emails sent, you can add another function to store the message into the SENT folder.
To store the sent message in the SENT folder, add this function to the PHP file. Make sure to use your mail server name for the hostname. In the following example it's imap.dreamhost.com:
<?php //The first line connects to your inbox over port 143 $mbox = imap_open("{imap.dreamhost.com:143/notls}INBOX", "me@example.com", "password"); //imap_append() appends a string to a mailbox. In this example your SENT folder. // Notice the 'r' format for the date function, which formats the date correctly for messaging. imap_append($mbox, "{imap.dreamhost.com:143/notls}INBOX.Sent", "From: me@example.com\r\n". "To: ".$to."\r\n". "Subject: ".$subject."\r\n". "Date: ".date("r", strtotime("now"))."\r\n". "\r\n". $body. "\r\n" ); // close mail connection. imap_close($mbox); ?>