如何使用PEAR Mail在单个联系表单中发送两封电子邮件

时间:2016-09-13 11:20:22

标签: php pear

我有联系表格,我想通过邮件接收邮件,同时发送邮件给客户。我有以下代码,它有一些工作。有时我收到消息并且客户端也收到消息,但有时只有一封电子邮件发送给客户或我,但从来没有发送过。

我在以下链接https://teamtreehouse.com/community/how-to-send-two-different-email-with-different-bodies-using-phpmailer中看过示例,但它使用的是PHP Mailer我希望使用相同的功能但是使用PEAR Mail。

我在这里看到了其他类似的问题PEAR Mail using gmail SMTP won't send 2 emails in sucession但是这个问题给出的答案并没有解决我的问题

以下是有效的代码。

<?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);  

    }
}
    ?>  

提前致谢

0 个答案:

没有答案