PHPMailer: Sending Email through PHP
For newer-generation servers, by default, when sending mail using PHP’s mail function, it disables sending mail out as the user “nobody.” So even if you have a “from” address configured, the envelope of the email will identify the mail as being sent from “nobody,” in which most mail servers will automatically identify it as being SPAM mail.
If you want to send mail with PHP, you will have to use SMPT with a valid username and password; and if you’re using a custom mailing script, then it’s best to do this using the PHPMailer class.
Here are some steps to demonstrate the basic method of using PHPMailer to send email from your web host’s servers (this example is based off of InMotion Hosting’s servers), which is intended for webmasters who are moderately familiar with PHP. There’s a possibility that you may have to make some adjustments to your script, which may be written differently, in order to let this modification work.
First, download PHPMailer from http://phpmailer.sourceforge.net, then unzip the archive and upload the resulting ‘phpmailer’ folder into your public_html.
Generally your setup would include a form being sent to a PHP script for processing. In this case, there is a basic HTML file that makes up a feedback form:
<form method="post" action="email.php"> Email: <input name="email" type="text" /><br /> Message:<br /> <textarea name="message" rows="15" cols="40"> </textarea><br /> <input type="submit" /> </form>
When this form is filled out, the information is passed over to “email.php,” which currently uses the mail function to send the message:
<?php $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; mail( "firstname.lastname@example.org", "Feedback Form Results", $message, "From: $email" ); header( "Location: http://www.domain.com/thankyou.html" ); ?>
To explain what variable we are defining here:
This tells the script that the method of sending is SMTP.
$mail->Host = "localhost";
This is the hostname of the mail server being used. For example, if you are sending from InMotion Hosting’s server, this can be localhost or mail.yourdomain.com.
$mail->From = "email@example.com";
This is the email address that the mail will appear to be from. This does not have to match your SMTP username, but must be a valid email address.
$mail->FromName = "Your Name";
This is the name that will appear as the sender of the email:
This is who will be receiving the form results. You can add as many recipients as you want simply by duplicating this line in the script.
$mail->SMTPAuth = "true";
This tells the script to use SMTP authentication, which is required to send mail through InMotion Hosting’s servers.
$mail->Username = "your@emailaddress"; $mail->Password = "yourpassword";
These are your login credentials for SMTP, which should reflect a valid email username and password. If this is an account you set up from your cPanel, your username will be your entire email address.
$mail->Subject = "Feedback form results";
This is the subject line of the email that will be sent.
$mail->Body = $message;
For the message body, you will need to rename $message with the variable that you created in your HTML form for the message field.
$email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ;
These were in the original form script, but simply defines the variables that can be passed from the HTML form. This is in good practice with register_globals being disabled for security.
Now when you send your form, the form will use SMTP as if it were being sent from a mail client.
Note that PHPMailer is very flexible and has more features than described here. For a listing of the optional variables that you can add, please read the documentation:
What are other methods have you used to send email?