0

I have a contact form and I want to receive message through mail and simultaneously send mail to the client. I have the below code it works some times. That is some times I get the message and client also get the message but sometimes only one email is sent either to client or to me but never both all the time.

I have seen example in the following link https://teamtreehouse.com/community/how-to-send-two-different-email-with-different-bodies-using-phpmailer but it uses PHP Mailer I want the same functionality but by using PEAR Mail.

I have seen other similar question over here PEAR Mail using gmail SMTP won't send 2 emails in sucession but the answer given in that question doesn't solve my problem

Below is the code which works some time.

<?php

require_once('Mail.php');
require_once('Mail/mime.php');

if (isset($_POST['Captcha'])) {
$name = filter_var($_POST['Name'] , FILTER_SANITIZE_STRING);
$email = filter_var($_POST['Email'] , FILTER_SANITIZE_EMAIL);
$phone = filter_var($_POST['Phone'] , FILTER_SANITIZE_STRING);
$budget = filter_var($_POST['Budget'] , FILTER_SANITIZE_STRING);
$timeline = filter_var($_POST['Timeline'] , FILTER_SANITIZE_STRING);
$description = filter_var($_POST['Description'] , FILTER_SANITIZE_STRING);
$spam = filter_var($_POST['usernameoftest'] , FILTER_SANITIZE_STRING); // Bot trap
if($spam) 
    {  // If the hidden field is not empty, it's a bot
                die("No spamming allowed bitch!"); 
    } else {
        $from = "sales@ganeshghate.com";
        $to = "ganeshghate@hotmail.com";
        $subject = "Message from : ".$name;
        $body = "Client's Name : ".$name."\r\n";
        $body .= "Client's Email ID : ".$email."\r\n";
        $body .= "Client's Phone Number : ".$phone."\r\n";
        $body .= "Client's Budget : ".$budget."\r\n";
        $body .= "Client's Timeline : ".$timeline."\r\n";
        $body .= "Client's Message : ".$description."\r\n";

        $host = "myhost";
        $port = "myport";
        $username = "myusername";  
        $password = "mypassword";
        $crlf = "\n";

        $headers = array ('From' => $from,
          'To' => $to,
          'Subject' => $subject);

        $mime = new Mail_mime($crlf);
        $mime->setTXTBody($body);
        $body = $mime->get();
        $headers = $mime->headers($headers);


        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
            $output["errorclientemail"] = "<div>
                                                <h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
                                                <h2>Oops there was some error we have not received your message</h2>
                                                <h2>Please report the above error to us via skype , email , phone details are on the left side</h2>
                                           </div>"."\r\n";
         } else {
            $output["successclientemail"] = "Thank you we will get back to you soon";

         }
        $bodyback = "We have received your message :".$body."\r\n";
        $bodyback .= "We will get back to you soon"."\r\n";
        $bodyback .= "With Regards "."\r\n";
        $bodyback .= "Ganesh Ghate "."\r\n";
        $subjectback = "Thank You for contacting us";
        $headersreply = array ('From' => $from,
          'To' => $email,
          'Subject' => $subjectback);

        $crlf1 = "\n";

        $mime = new Mail_mime($crlf1);
        $mime->setTXTBody($bodyback);
        $bodyback = $mime->get();
        $headersreply = $mime->headers($headersreply);

        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

        $mail = $smtp->send($email, $headersreply, $bodyback);

        if (PEAR::isError($mail)) {
            $output["errorreplyemail"] = "<div>
                                                <h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
                                                <h2>Oops there was some error we have not delivered confirmation email to you</h2>
                                                <h2>Please report the above erorr to us via skype , email , phone details are on the left side</h2>
                                          </div>"."\r\n";
         } else {
            $output["successreplyemail"] = "We have sent confirmation email to you. Please check your inbox / junk mail";
         }


    echo json_encode($output);  

    }
}
    ?>  

Thanks in advance

Community
  • 1
  • 1
Ganesh G
  • 61
  • 1
  • 1
  • 5
  • do you get error messages when it does not work? what do the mail server logs say? does it fail and work on the same addresses? – cweiske Oct 04 '16 at 06:29
  • I have hosting with godaddy the error log they provided was two years old and I have rectified the errors given by them like 'Net_SMTP' not found on line 349 of smtp.php. I have rectified that problem now I spoke to them they said they will update the error log so till then I have to wait. And yes it fails and work on the same addresses – Ganesh G Oct 05 '16 at 12:13
  • I have tried two different credentials like two different port,host,username,password still the same problem – Ganesh G Oct 05 '16 at 12:20

0 Answers0