We Share The Best Web Design Rersources On The Net. Join Us Its FREE!

A Contact Form With SMTP and PHP

Rate this post

Imagine that your web hosting provider don’t allow phpmail function and you have to use SMTP (Simple Mail Transfer Protocol)! So here is the solution : A simple contact form with SMTP and PHP that is easy to add on your website and yo configure.

On this simple tutorial, you will learn how to use a SMTP Contact form with some easy php code! and please if you find this useful, share it with your friends on social networks!

SMTP and PHP Contact Form

The script contain 3 files :

– contact.php
– Mysmtpconfig.php // SMTP Server Configuration
– smtpclass.php // SMTP Mail Sending Class

On Mysmtpconfig.php files you have to change SMTP server details :

//Server Address
$SmtpPort="25"; //default

The smtpclass.php file :

class SMTPClient

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)

$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;

if ($SmtpPort == "") 
$this->PortSMTP = 25;
$this->PortSMTP = $SmtpPort;

function SendMail ()
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n"); 
$talk["hello"] = fgets ( $SMTPIN, 1024 ); 
fputs($SMTPIN, "auth login\r\n");
fputs($SMTPIN, $this->SmtpUser."\r\n");
fputs($SMTPIN, $this->SmtpPass."\r\n");
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n"); 
$talk["From"] = fgets ( $SMTPIN, 1024 ); 
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n"); 
$talk["To"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
fputs ($SMTPIN, "QUIT\r\n"); 
return $talk;

And our contact.php file :

$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['sub'];
$body = $_POST['message'];
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
<form method="post" action="">
To:<input type="text" name="to" />
From :<input type='text' name="from" />
Subject :<input type='text' name="sub" />
Message :<textarea name="message"></textarea>
<input type="submit" value=" Send " />

Download The Script

At seodesign we believe that we help designers and developers to find the right inspiration before create their works so If you would like to be kept up to date with our posts and free web design resource.

Leave a Comment

Your email address will not be published. Required fields are marked *