How to configure a Contact Form with us

There are many benefits of using a contact form versus posting an actual email address on your website:

  • With a contact form, you can request specific information from your clients (e.g. phone number or company name).
  • Customers are more likely to contact you if they can easily find and submit the contact form without having to open their own email program.
  • The most important reason for using a contact form is to reduce incoming spam. When you display your email address on your website, there is a good chance that it might end up in the hands of a spammer. For extra security, you can also add reCAPTCHA to your form.

Contact forms can use PHP mail() function or SMTP authentication to send emails. With the PHP mail() function, you are sending emails directly from your web server without any form of authentication. Unlike the PHP mail(), SMTP authentication let you use the mail server of your choice by specifying the username and password of the email account. If the details are valid, the email is accepted and delivered to the designated recipient.


PHP mail() function

SMTP authentication

Using Gmail in a contact form

Configuring SMTP in WordPress




PHP mail() function


If your script uses PHP mail() function, you have to configure the From field of the form with an email account that was created in cPanel, is hosted on our server and has Local Mail Exchanger. We took this measure to prevent sending spam using forums, guest books, and contact form scripts. If you want to use a third-party email address in the From field (like Gmail, Private Email, etc) or avoid 'via cPanel user@server' part, SMTP authentication is required.

If your domain uses a third-party email service, but you still want to configure PHP mail() you can create a subdomain and an email account for the subdomain and set it with local email routing.

To set email routing to Local, go to cPanel >> the Email section >> the Email Routing menu:



Select your subdomain (or domain) from the drop-down list, select Local Mail Exchanger, and click Change:



An example of simple PHP mail() code:

$mail_to_send_to = "name@anydomain.tld";
$from_email = "from@yourdomain.tld";
$sendflag = $_REQUEST['sendflag'];                      
if ( $sendflag == "send" )
        {
                $email = $_REQUEST['email'] ;
                $message = $_REQUEST['message'] ;
                $headers = "From: $from_email" . "\r\n" . "Reply-To: $email" . "\r\n" ;
                $a = mail( $mail_to_send_to, "Message from a contact form", $message, $headers );
                if ($a)
                {
                     print("Message was sent, you can send another one");
                } else {
                     print("Message wasn't sent, please check that you have changed emails in the bottom");
                }
        }


In the code above:
  • $mail_to_send_to = "name@anydomain.tld" identifies the email address the message will be sent to. It can be any email address, including third-party services like Gmail, Yahoo, etc.
  • $from_email = "from@yourdomain.tld" identifies the email address the message will be sent from. It should be a local email address (created in cPanel)
  • Reply-To: $email" is the email address of the site visitor, assuming they specify one in the "Your email" field of the contact form. This email is then assigned as the "Reply To" address.



SMTP authentication


Simple Mail Transfer Protocol is a standard protocol used for sending emails. SMTP uses authentication (a login with a username and password) which increases email deliverability.

To use SMTP authentication you need:
  • username (your full email address in most cases);
  • password (password from that email account);
  • hostname (server from which the emails are sent);
  • port (465 with SSL, 587/25 with TLS);
  • type of encrypted connection: SSL or TLS.

NOTE: Outgoing ports are closed by default on our Shared and Reseller Hosting servers due to security reasons. If you want to connect to third-party email servers, contact our support to open SMTP ports for your account. You don't need to open ports if you use SMTP with local email address.

The SMTP mail code looks like this:

function sendmail($to,$nameto,$subject,$message,$altmess)  {
  $from  = "youremail@domain.tld";
  $mail = new PHPMailer(); 
  $mail->CharSet = 'UTF-8';
  $mail->isSMTP();  
  $mail->SMTPAuth   = true;  
  $mail->Host       = "smtp.host.tld";
  $mail->Port       = 465;
  $mail->Username   = $from; 
  $mail->Password   = "pass123";
  $mail->SMTPSecure = "ssl";    
  $mail->setFrom($from,$namefrom);  
  $mail->addCC($from,$namefrom);     
  $mail->Subject  = $subject;
  $mail->AltBody  = $altmess;
  $mail->Body = $message;
    $mail->addAddress($to, $nameto);
  return $mail->send();
}


In the code above:
  • $mail->Username is your full email address
  • $mail->Password is the password to that email account
  • $mail->Host is the hostname of the server that sends the emails
  • $mail->Port is the SMTP port
  • $mail->SMTPSecure is the type of the encryption



Using Gmail in a contact form


To be able to use a Gmail email address in your contact form, you need to make some changes in your Gmail account settings. This only applies if you want to send From the Gmail account. Skip this part if you use Gmail only in the To field.

Google may block sign-in attempts from IPs which are located far from the usual geographical location of the mailbox owner. In such case, the “Suspicious sign in is prevented” email will appear in the Gmail inbox.

There are three steps:

1. Sign into your Gmail account at https://myaccount.google.com/security?pli=1#signin and find the Password & sign-in method section. Turn 2-Step Verification off, if you are using it (mail script will not be able to log in to the account when 2FA is on):



2. Scroll down to the Apps with account access section and set Allow less secure apps: ON:



3. Next, go to https://accounts.google.com/DisplayUnlockCaptcha and click Continue:



Now you can configure and test your contact form. Use the following SMTP settings:
  • Email sending method: SMTP;
  • SMTP authentication: enabled;
  • SMTP username: yourmailbox@gmail.com;
  • SMTP password: password for this email address;
  • SMTP hostname: smtp.gmail.com;
  • SMTP port: 465 with SSL on.



SMTP in WordPress


By default, WordPress uses PHP mail() function to send emails. If you want to send WordPress mail from a third-party mail server or use SMTP authentication with your cPanel email account, you need to install an SMTP plugin. There are several SMTP plugins for WordPress. We will use the WP Mail SMTP plugin in this example.

1. Log into your WordPress admin dashboard and go to Plugins >> Add New >> search for the WP Mail SMTP plugin and click Install Now:



2. Once installed, click Activate. Then, go to Settings >> WP Mail SMTP to configure the plugin settings:



3. You can find an example of the SMTP setup below:



  • From Email - the email address you want to use to send all WordPress emails from
  • From Name - the name that the emails will be sent from
  • Mailer - choose Other
  • SMTP Host - hostname of the outgoing mail server (smtp.gmail.com for Gmail; mail.privateemail.com for Private Email; smtp.yahoo.com for Yahoo mail; server your account is hosted on for cPanel email, etc.)
  • Encryption - TLS or SSL
  • SMTP Port - 465 (SSL) or 587 (TLS). (NOTE: If you are setting a plugin third-party email service, contact our support to open SMTP ports for your account)
  • Auto TLS - if your server supports TLS encryption, you may enable this option
  • Authentication - On
  • SMTP Username - full email address
  • SMTP Password - password for that email address

Enter all the details and click Save Settings.

To test the configuration, go to the Email Test tab, enter your email and click the Send Email button. Check your email for the test email:



For more information on how to set up WordPress Contact Form 7, click here.


That's it!




Need any help? Contact our HelpDesk


Comments

We welcome your comments, questions, corrections and additional information relating to this article. Your comments may take some time to appear. Please be aware that off-topic comments will be deleted.

If you need specific help with your account, feel free to contact our Support Team. Thank you.

Need help? We're always here for you.

× Close