i am working on contact form using SMTP. I am unable to send a mail. Please help me to fix the issue.


<div id="frmContact" class="w-form">
            <div id="mail-status"></div>
            <!-- <form action="contact.php" method="post">-->
            <label for="name">Name:</label>
             <input class="w-input" type="text" placeholder="Enter your name" id="cf_name" name="cf_name">
           <label for="email"><p class="info">*</p>Email Address: <span id="cf_email-info" class="info"></span></label>
             <input class="w-input" placeholder="Enter your email address" type="text" id="cf_email" name="cf_email">
           <label for="email">Your Message:</label>
             <textarea class="w-input message" placeholder="Enter your Message Here" id="cf_message" name="cf_message"></textarea><br>
                     <button name="submit" class="w-button" onClick="sendContact();">Send</button>

           <!-- <input class="w-button" type="submit" value="Send">-->
          <div class="w-form-done">
              <p>Thank you! Your submission has been received!</p>
            <div class="w-form-fail">
              <p>Oops! Something went wrong while submitting the form :(</p>
<script type="text/javascript">
        function sendContact() {
            var valid;  
            valid = validateContact();
            if(valid) {
                    url: "contact.php",
                    type: "POST",
                    error:function (){}

        function validateContact() {
            var valid = true;   

            if(!$("#cf_email").val()) {
                valid = false;
            if(!$("#cf_email").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
                valid = false;

            return valid;




require "classemail.php";

$mail = new EMail;
$mail->Username = 'webmaster@domainname.com';
$mail->Password = 'password';

$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'contact@domainname.com';
$subject = 'Message from a contant - '.$field_name;

$mail->SetFrom("webmaster@domainname.com","Webmaster");     // Name is optional
$mail->AddTo($field_email,$field_name); // Name is optional
$mail->Subject = $field_name . ' - From contact';
$mail->Message = "<div style='width:600px; margin:0px auto;'>
           <div style='width:600px; height:auto; border-top:1px solid #dfdfdf; border-left:1px solid #dfdfdf; border-right:1px solid #dfdfdf; border-bottom:3px solid #da251e; float:left;'><img src='http://domainname.com/images/logo.png'></div><br>
           <div style='width:560px; height:auto; border-left:1px solid #dfdfdf; border-right:1px solid #dfdfdf; padding:20px 20px; background-color:#f7f7f7; float:left;'>
           <table width='450' border='0'>
           <tr><td colspan=2><span style='font-family:Arial; font-size:12px; color:#2A4E98; font-weight:bold;'>Dear Sir / Madam</span></td></tr>
           <tr><td align=left colspan=2><span style='font-family:Arial; font-size:12px; color:#000000;text-indent:40px;'></span></td></tr>
           <tr><td align=left colspan=2>&nbsp;</td></tr>
           <tr><td align=left colspan=2><b><u>My Contact Details :</u></b></td></tr>
           <tr><td align=left width='280'> Contact Name : &nbsp;</td><td align=left width='280'>" .$field_name. "</td></tr>
           <tr><td align=left> Email : &nbsp;</td><td align=left> " .$field_email. "</td></tr>
           <tr><td align=left> Information Requested or Your Question: &nbsp;</td><td align=left> " .$field_message."</td></tr>     
           <tr><td align=left colspan=2>&nbsp;</td></tr>
           <tr><td align=left colspan=2>&nbsp;</td></tr>
           <tr><td align=left colspan=2>Thanks & Regards,</td></tr>
           <tr><td align=left colspan=2> " .$field_name. "</td></tr>
           <div style=' float:left; width:586px; height:30px; border-top:3px solid #da251e; background-color:#dfdfdf;border-left:1px solid #c5c5c5; border-right:1px solid #c5c5c5; border-bottom:1px solid #c5c5c5; float:left; line-height:30px; padding-left:15px; font-family:Arial; font-size:10px; color:#828282;'> Copyright ".date('Y')." Domain Name, Inc. All rights reserved.</div></div>" ;

//Optional stuff
//$mail->AddCc("ccaddress@example.com","CC Name");  // Set a CC if needed, name optional
$mail->ContentType = "text/html";               // Defaults to "text/plain; charset=iso-8859-1"
$mail->Headers['X-SomeHeader'] = 'abcde';       // Set some extra headers if required
$mail->ConnectTimeout = 30;     // Socket connect timeout (sec)
$mail->ResponseTimeout = 8;     // CMD response timeout (sec)

$success = $mail->Send();
if ($success) 
    print "<p class='success'>Thank you! Your submission has been received!</p>";
    print "<p class='Error'>Oops! Something went wrong while submitting the form :(</p>";     


Recommended Answers

All 3 Replies

I am unable to send a mail. Please help me to fix the issue.

Hi, unable in which sense? The script fails the connection with the SMTP or something else? Do you have PHP error logs or SMTP error logs?

Replace lines below and including 47 with:

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";

You should get some additional information about the error. You can also raise the debug level through $mail->SMTPDebug = 2;.

Some notes about the debugging with PHPMailer:

thanks cereal



* Class Email.php Used to send email via SMTP authentication

class EMail
  const newline = "\r\n";

    $Server, $Port, $Localhost,

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

  function __construct()
    $this->Server = "mail.server.com"; // get replaced with your SMTP server
    $this->Port = 25; // replace this with SMTP Port
    $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");


    return substr($this->Log['data2'],0,3) == "250";

i am getting below error.

The server encountered an internal error or misconfiguration and was unable to complete your request.Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error. More information about this error may be available in the server error log.

Sorry, due to the PHPMailer tag I thought you were using the library.

From what I see you're using fsockopen() which works, but depending on the authentication mode your SMTP client must be required to reply to the server, before actually sending the message and, if this does not happen, then it will throw an error and close the connection. For this reason you should check the SMTP server logs.

If you can't, then try to use the linked library, it handles multiple authentication methods and it allows to debug easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.