Along with the PHP mail() function and solutions such as FormMail, there is also phpMailer that can be used to send mail through your website. In this article we will walk you through the process of sending mail using phpMailer.
to begin using PHPMailer, you will first need to download it. You may locate PHPMailer through this direct download from their page on Github.
After you have downloaded the zip file, you will want to unzip and extract it to your public_html directory within your hosting account. When using cPanel to extract the zip file, it will place all files within a directory labeled PHPMailer-master. Any file paths in this article will reference that directory but feel free to adjust it as you see fit. After the files are uploaded and extracted, you will then edit your site to use the PHPMailer code.
As an email script won't be very helpful without a method to send the email, you will now create a contact form on your site. The following is an example of a basic contact form that you can place within the desired page to handle anyone who wants to send you mail.
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
As you can see, the form action is set to email.php. This form action determines how the information that is submitted to the form is processed and determines what happens once the form is submitted.
As the information from the form is passed over to the email.php script, we will need to place some code there. Here, we will provide the an example code you can use for your contact form. Be sure to read over the comments and adjust as you see fit.
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("PHPMailer-master/lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email:
This email address is being protected from spambots. You need JavaScript enabled to view it.
// pass: password
$mail->Username = "
This email address is being protected from spambots. You need JavaScript enabled to view it.
"; // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("
This email address is being protected from spambots. You need JavaScript enabled to view it.
", "Jeff Matson");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
When a user visits your contact form and sends a message, it will then pass the information over to the email.php file that is defined as the form action, which will then send the email.
PHPMailer has a very large number of options to fit your individual needs. You may want to view more information on particular options on the PHPMailer Github project page.
3 Points
|
2012-06-22 4:28 pm
It's a very good explanation for a novice like me who was gropping for the right direction. Thanks for posting
|
n/a Points
|
2014-12-12 10:32 am
I came to realize that this program often fails to deliver messages when website viewers put gmail/yahoo mail address in the form input. How can I solve this issue? |
Staff 2,342 Points
|
2014-12-12 6:15 pm
This can vary based on how the recipient's mail provider is handling mail. Are you getting a bounceback message? Is the mail being delivered to the user's spam folder?
|
n/a Points
|
2015-04-18 3:36 am
As of Apr 2015, this does not work on WebHostingHub. If you set "$mail->SMTPDebug = 2; ", you will see that the entire transcript of the interaction. It exactly replicates what you might do by hand successfully with telnet or openssl, but you still get 535 user authentication failures with phpMail. I don't know why. Something about the smtp protocol in current use. Here's a workaround: 1) visit - http://www.9lessons.info/2011/07/send-mail-using-smtp-and-php.html 2) In addition to the displayed code, you will need smtp.php and sasl.php. You can get them by clicking "Download Script" 3) You will also need an sasl client. I used "PLAIN" - that client can be found at: https://github.com/jhigman/TalisRdfEditor/blob/master/lib/sasl/plain_sasl_client.php 4) Upload your php script, and the three support scripts to the appropriate place (try your webroot as a start) 5) See below code fragment, edited from Step 1 code, verified to work on my WebHostingHub account. // configuration that works for WebHostingHub - as of Apr 2015 require("smtp.php"); require("sasl.php"); //SASL authentication $smtp=new smtp_class; $smtp->host_name="www.yourWebHostingHubSite.com"; $smtp->host_port=587; // not secure - but it's running on the host's server system $smtp->ssl=0; $smtp->start_tls=0; //which ports use which protocols? I don't know $smtp->localhost="localhost"; $smtp->direct_delivery=0; $smtp->timeout=10; $smtp->data_timeout=0; $smtp->debug=1; // if ==1, will show php include path, where they should be //if you don't put the support files in the right place $smtp->html_debug=1; $smtp->pop3_auth_host=""; $smtp->user="anActiveAccount@yourWebHostingHubSite.com"; // SMTP Username $smtp->realm=""; $smtp->password="ObviousPassword"; // SMTP Password for anActiveAccount on WebHostingHub $smtp->workstation=""; $smtp->authentication_mechanism="PLAIN";
Thanks to Brett T. on the support team for providing the link for Step 1. |
n/a Points
|
2016-01-22 1:28 pm
i'm trying to send mail but am getting the folwing errror SMTP -> ERROR: Failed to connect to server: connction timeout (110) The following From address failed: shiva@wikeventx.com
my smtp:secure48.webhostinghub.com port:465 |
Staff 1,198 Points
|
2016-01-23 10:58 am
Hello Shiva,
Did you follow the steps exactly as it should work on any server. Best Regards, TJ Edens |
n/a Points
|
2017-02-19 3:03 am
Hello there. This does not seem to work on my end. The issue seemed to be that I did not tell the program what type of encryption I was using. For it to work, add: $mail->SMTPSecure = "tls"; if the port is 587 OR $mail->SMTPSecure = 'ssl';if the port is 465 Good luck, everyone. |
Staff 304 Points
|
2017-02-21 2:01 am
Ed, thank you for sharing this updated information!
|
Email: | support@WebHostingHub.com | Ticket: | Submit a Support Ticket |
---|---|---|---|
Call: |
877-595-4HUB (4482) 757-416-6627 (Intl.) |
Chat: | Click To Chat Now |
We value your feedback!
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
new! - Enter your name and email address above and we will post your feedback in the comments on this page!