2016-06-24 12 views
0

私は現在、お見積りフォームを作成中です。フォームを送信した後、提出されたレコードをコピーとしてクライアントに送り返すように設定された電子メールがありました。しかし、私はトランザクション電子メールのためにMandrillを使用していたので、もはや無料ではありません。有料のサービスを使用したくないので、PHPMailerを使用して同じメールを送信しようとしています。私はそれが電子メールを送信するためにPHPを呼び出すことを理解するのが難しいと思っている。どんな助け?私はオプションのために開いています。フォーム提出後にメールを送信したいだけです。データベースにデータを投稿した後にPHPメールを送信する方法

email.js

 function finalize_things(data, order_id){ 

     //posting data to the server side page 
     $.post(window.location.origin+'/post/', data,function(response){ 
       if(response == 'success'){ 
       console.log(response); 
       send_email(order_id, data.email, data.firstname, data.project_details, data.file_names); 

      } else { 

       console.log(response); 
       alert('order not placed.');} 
     }); 
     } 


    function send_email(order_id, email, firstname, project_details, file_names){ 

      var html_message = "message here" 

     data = { 

       "key": "xxxxxxxxx", 
       "message": { 

        "html": html_message, 
        "text": "", 
        "subject": "Your subject", 
        "from_email": "[email protected]", 
        "from_name": "Company name", 

        "to": [{ 
         "email": email, 
         "name": firstname 
         } 
        ]}, 

       "async": false 
      }; 

     $.ajax({url: 'https://mandrillapp.com/api/1.0/messages/send.json', 

       type: 'POST', 

       data: data, 

       success: function(resp){ 

        console.log(resp); } 
       }); 

phpmailerのコード:

だから、
 include_once '/../../PHPMailer/cms_db.php'; 
     require_once '/../../PHPMailer/PHPMailerAutoload.php'; 
     require_once '/../../PHPMailer/config.php'; 

     if (isset($_POST["email"]) && !empty($_POST["email"])) { 


     $mail = new PHPMailer; 

     $mail->isSMTP();       // Set mailer to use SMTP 
     $mail->SMTPAuth = true;      // Enable SMTP authentication 

     $mail->Host = $smtp_server;     // Specify main and backup SMTP servers 
     $mail->Username = $username;     // SMTP username 
     $mail->Password = $password;    // SMTP password 
     $mail->SMTPSecure = "tls";     // Enable TLS encryption, `ssl` also accepted 
     $mail->Port = 587;       // TCP port to connect to 

     $mail->setFrom("[email protected]", "Company Name"); 

     $mail->addAddress($email, $firstname); // Add a recipient 

     $mail->isHTML(true); // Set email format to HTML 

     $bodyContent = "test message"; 

     $mail->Subject = "Your Quote ID"; 
     $mail->Body = $bodyContent; 
     $mail->AltBody = $bodyContent; 

     if(!$mail->send()) { 
      echo "Message could not be sent."; 
      $error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>'; 
     } else { 
      $error = '<div class="alert alert-success"><strong>Message has been sent</strong></div>'; 
     } 
     } 
+1

あるどの部分あなたは理解していない?より具体的になりますか? – Confiqure

+0

JavaScriptでPHP電子メールコードを使用するにはどうすればよいですか? console.logの後と同様に、電子メールを送信し、phpmailerでデータを投稿した後で電子メールを送信する方法を教えてください。 $ .AJAX ..とはどうすればいいですか? –

答えて

1

、あなたがGmailなどの外部サービスを使用したくない場合は...あなたがあなた自身のメールサーバーを設定する必要があります(postfix、sendmailなど)注意してください。ウェブサーバーには、すでに頻繁に(httpd + php + mysql + postfix)のpotfixがあります。 あなたのホスティングポストフィックスは既にありますか?このコードに基づいて独自のスクリプトを記述するかphpmailerのを使用します(これはローカルのメールデーモンのための設定があります)

PS phpmailerの送信することができます - それが動作した場合

<?php 
$to = '[email protected]'; 
$subject = 'test subj'; 
$mail_body = 'test body'; 
mail($to, $subject, $mail_body); 
?> 

:ようstandard mail function、PHPスクリプトを使用してこれをチェックGmailのhttps://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps(キーここではライン:$mail->isSMTP();)のようなextenal Webサーバを経由してメールなどのpostfixなどやローカルサーバーを経由して、 - を参照してexample here - キーラインは$mail->isSendmail(); であり、あなたの現在のコードスニペットは、LOCALのためではない、外部サービスを利用するためであります!

$.ajax({url: 'https://mandrillapp.com/api/1.0/messages/send.json',

$.ajax({url: 'http://yourserver.tld/send_phpmailer.php',

そしてsend_phpmailer.phpにこのようなものを使用する:あなたのjsの中

+0

ありがとうございます。私はbluehostでWebホスティングをしているので、SMTPで電子メールを送ることができます。唯一の問題は...私はちょうど私のemail.jsコードにこのコードを追加する方法は、任意のクライアントがフォームを提出すると、電子メールを送信するこのPHPのメーラーコードを実行したい...あなたはemail.jsコードを変更することはできますGmailのAPIを使ってうまく動作するようになりました....マンドリルでは、JSON APIを使用しました。 –

0

変更この行を

<?php 

////// Part 1. Set up variables 

$key = $_POST['key']; // need for mandrill, you can omit this 
$html = $_POST['message']['html']; 
$text = $_POST['message']['text']; 
$subject = $_POST['message']['subject']; 
$from_email = $_POST['message']['from_email']; 
$from_name = $_POST['message']['from_name']; 
$to_email = $_POST['message']['to']['email']; 
$to_name = $_POST['message']['to']['name']; 

////// Part 2. Including PHPMailer 
include_once '/../../PHPMailer/cms_db.php'; 
require_once '/../../PHPMailer/PHPMailerAutoload.php'; 
require_once '/../../PHPMailer/config.php'; 

////// Part 3. Sending letter by PHPMailer 
$mail = new PHPMailer; 

$mail->isSMTP(); 

//Enable SMTP debugging 
// 0 = off (for production use) 
// 1 = client messages 
// 2 = client and server messages 
$mail->SMTPDebug = 2; 

$mail->Debugoutput = 'html'; 

$mail->Host = 'smtp.gmail.com'; 

$mail->Port = 587; 

$mail->SMTPSecure = 'tls'; 

$mail->SMTPAuth = true; 

$mail->Username = "[email protected]"; 
$mail->Password = "yourpassword"; 

//Set who the message is to be sent from 
$mail->setFrom($from_email, $from_name); 

//Set an alternative reply-to address 
//$mail->addReplyTo('[email protected]', 'First Last'); 

//Set who the message is to be sent to 
$mail->addAddress($to_email, $to_name); 

//Set the subject line 
$mail->Subject = $subject; 

//Read an HTML message body from an external file, convert referenced images to embedded, 
//convert HTML into a basic plain-text alternative body 
$mail->msgHTML($html); 

//Replace the plain text body with one created manually 
$mail->AltBody = $text; 

//Attach an image file 
//$mail->addAttachment('images/phpmailer_mini.png'); 

//send the message, check for errors 
if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

?> 
+0

ありがとう、私はそれを試してみましょう。完全なパスの代わりに直接ページ名を使用できますか? $ .ajaxのように({url: 'send_phpmailer.php'、email.jsとsend_phpmailerの両方が同じフォルダにあるため) –

+0

もちろん、jsとphpが同じフォルダにある場合にのみファイル名を使用できます。 –

関連する問題