PHPMailer: Sending Email through PHP

Print Article Print Article

PHPMailer: Sending Email through PHP

By Web Hosting Help Guy

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.

Email Image

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( "me@mydomain.com", "Feedback Form Results",
    $message, "From: $email" );
  header( "Location: http://www.domain.com/thankyou.html" );
?>

To explain what variable we are defining here:

$mail->IsSMTP();

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 = "your@emailaddress.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:

$mail->AddAddress("recipient_1@domain.com");

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:

How To Send Email Using phpMailer

http://phpmailer.sourceforge.net/docs/

What are other methods have you used to send email?

Comments

Katie January 12th, 2011 at 2:55 pm

I am trying to send a form using phpMailer, and I’ve followed the instructions from the help file here on inmotion hosting. (https://support.inmotionhosting.com/cgi-bin/kb.cgi?do=read&id=78&lang=en)
I keep getting a parse error once the form sends. I didn’t alter the class.phpmailer.php file at all I just uploaded it to the phpmailer folder.
This is what it says:

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/username/public_html/phpmailer/class.phpmailer.php on line 53

Line 53 in dreamweaver is
public $Priority = 3;

Am I missing something? Should this be changed?
Thanks.

Web Hosting Help Guy January 12th, 2011 at 3:58 pm

Hi Katie,

You might want to try using a different version of PHPMailer, in your case try version 4, because the server you are on may be using a different version of PHP that doesn’t align with your current version of PHPMailer.

If that doesn’t work or you’re already using PHPMailer for PHP4, then please email InMotion Hosting’s Support Department at support@inmotionhosting.com or call 888-321-4678 ext.2.

Hope this works out for you!

Leave A Comment

Name *required

Email *not published *required

Website