Cara Membuat Contact Form dengan SMTP Authentication

Membuat contact form di website itu penting, lho. Selain untuk memudahkan pengunjung, ini juga penting untuk mendapatkan data leads.

Supaya nanti bisa kita gunakan nih untuk meraih konversi yang tinggi. Nah, cara membuat contact form ini pun gampang banget, kok.

Kalau di WordPress sendiri, kita tinggal menggunakan plugin saja. Tapi, kalau mau secara manual, kita bisa membuatnya dengan menggunakan SMTP authentication.

Nah, kali ini kita akan membuat contact form dengan SMTP authentication. Tenang saja, caranya juga gampang, kok.

Supaya nggak bingung, simak cara membuat contact form dengan SMTP authentication di cPanel berikut ini, yuk!

1. Login ke akun cPanel kita.

login ke cpanel

 

2. Pilih menu File Manager di bagian Files.

cara membuat contact form

 

3. Pilih folder public_html tempat website kita tersimpan. Lalu, klik +File di bagian atas.

cara membuat contact form

 

4. Masukkan email.php di bagian New File Name. Lalu, klik Create New File.

cara membuat contact form

 

5. Klik kanan pada file email.php dan pilih Edit. Lalu, masukkan script berikut ini:

 

<?php

class EMail
{
const newline = “\r\n”;

private
$Server, $Port, $Localhost,
$skt;

public
$Username, $Password, $ConnectTimeout, $ResponseTimeout,
$Headers, $ContentType, $From, $To, $Cc, $Subject, $Message,
$Log;

function __construct()
{
$this->Server = “mail.nama.domains“;
$this->Port = 587;
$this->Localhost = “localhost”;
$this->ConnectTimeout = 30;
$this->ResponseTimeout = 8;
$this->From = array();
$this->To = array();
$this->Cc = array();
$this->Log = array();
$this->Headers[‘MIME-Version’] = “1.0”;
$this->Headers[‘Content-type’] = “text/plain; charset=iso-8859-1?;
}

private function GetResponse()
{
stream_set_timeout($this->skt, $this->ResponseTimeout);
$response = ”;
while (($line = fgets($this->skt, 515)) != false)
{
$response .= trim($line) . “\n”;
if (substr($line,3,1)==’ ‘) break;
}
return trim($response);
}

private function SendCMD($CMD)
{
fputs($this->skt, $CMD . self::newline);

return $this->GetResponse();
}

private function FmtAddr(&$addr)
{
if ($addr[1] == “”) return $addr[0]; else return “\”{$addr[1]}\” <{$addr[0]}>”;
}

private function FmtAddrList(&$addrs)
{
$list = “”;
foreach ($addrs as $addr)
{
if ($list) $list .= “, “.self::newline.”\t”;
$list .= $this->FmtAddr($addr);
}
return $list;
}

function AddTo($addr,$name = “”)
{
$this->To[] = array($addr,$name);
}

function AddCc($addr,$name = “”)
{
$this->Cc[] = array($addr,$name);
}

function SetFrom($addr,$name = “”)
{
$this->From = array($addr,$name);
}

function Send()
{
$newLine = self::newline;

//Connect to the host on the specified port
$this->skt = fsockopen($this->Server, $this->Port, $errno, $errstr, $this->ConnectTimeout);

if (empty($this->skt))
return false;

$this->Log[‘connection’] = $this->GetResponse();

//Say Hello to SMTP
$this->Log[‘helo’] = $this->SendCMD(“EHLO {$this->Localhost}”);

//Request Auth Login
$this->Log[‘auth’] = $this->SendCMD(“AUTH LOGIN”);
$this->Log[‘username’] = $this->SendCMD(base64_encode($this->Username));
$this->Log[‘password’] = $this->SendCMD(base64_encode($this->Password));

//Email From
$this->Log[‘mailfrom’] = $this->SendCMD(“MAIL FROM:<{$this->From[0]}>”);

//Email To
$i = 1;
foreach (array_merge($this->To,$this->Cc) as $addr)
$this->Log[‘rcptto’.$i++] = $this->SendCMD(“RCPT TO:<{$addr[0]}>”);

//The Email
$this->Log[‘data1’] = $this->SendCMD(“DATA”);

//Construct Headers
if (!empty($this->ContentType))
$this->Headers[‘Content-type’] = $this->ContentType;
$this->Headers[‘From’] = $this->FmtAddr($this->From);
$this->Headers[‘To’] = $this->FmtAddrList($this->To);
if (!empty($this->Cc))
$this->Headers[‘Cc’] = $this->FmtAddrList($this->Cc);
$this->Headers[‘Subject’] = $this->Subject;
$this->Headers[‘Date’] = date(‘r’);

$headers = ”;
foreach ($this->Headers as $key => $val)
$headers .= $key . ‘: ‘ . $val . self::newline;

$this->Log[‘data2’] = $this->SendCMD(“{$headers}{$newLine}{$this->Message}{$newLine}.”);

// Say Bye to SMTP
$this->Log[‘quit’] = $this->SendCMD(“QUIT”);

fclose($this->skt);

return substr($this->Log[‘data2’],0,3) == “250”;
}
}
?>

 

6. Buat kembali file baru di File Manager dengan nama pageemail.php. Lalu, buka file dan masukkan script berikut ini:

<?php
require “email.php“;

$mail = new EMail;
$mail->Username = ‘akunemailanda@nama.domains‘;
$mail->Password = ‘passwordakunemail‘;

$mail->SetFrom($_REQUEST[’email’],$_REQUEST[‘from’]); // Nama bersifat opsional
$mail->AddTo(“emailtujuan@nama.domains“,”Email Admin/kontak Website“); // Nama bersifat opsional
$mail->Subject = $_REQUEST[‘subject’];
$mail->Message = $_REQUEST[‘message’];

//Optional stuff
$mail->AddCc(“emailkedua@nama.domains“,””); // jika dibutuhkan
$mail->ContentType = “text/html”;
$mail->Headers[‘X-SomeHeader’] = ‘abcde’;
$mail->ConnectTimeout = 30;
$mail->ResponseTimeout = 8;

$success = $mail->Send();

?>

 

7. Buat bagian kolom form dengan membuat file contact.html di File Manager. Lalu, buka file dan masukkan script berikut ini:

<form method=”post” action=”pageemail.php“>
From: <input name=”from” id=”from” type=”text” /><br />
Email: <input name=”email” id=”email” type=”text” /><br />
Subject: <input name=”subject” id=”subject” type=”text” /><br />
Message:<br />
<textarea name=”message” id=”message” rows=”15? cols=”40?></textarea><br />

<input type=”submit” value=”Submit” />
</form>

8. Coba akses contact form dengan namadomain/contact.html.

 

Sampai di sini, cara membuat contact form dengan SMTP authentication di cPanel sudah selesai nih. Gampang, kan?

Jika ada pertanyaan lain, langsung saja hubungi tim kami melalui Live Chat, Call Center atau Support Ticket, ya.

Cek juga artikel tutorial kami yang lain dengan klik di sini!