2017-08-31 3 views
0

私はSendGridを使用して電子メールを送信するPHPスクリプトを使用しています。私が$ _POSTコマンドを取り除くならば、代わりに手動で値を設定し、ローカルで(cmdから)実行してください。しかし、ajaxを使用してフォームを送信しようとすると、500 Internal Server Errorが発生します。ここでphpスクリプトはローカルで動作しますが、ajax呼び出しでは500の内部サーバーエラーが発生します

ここでは、PHPコード

<?php require 'vendor/autoload.php'; 
header("Access-Control-Allow-Origin: *"); 

if(!(isset($_POST['email']) && isset($_POST['messageType']) && isset($_POST['projectName']) && isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['message']))){ 
    echo "Unable to process request"; 
    return; 
} 

$email = $_POST['email']; 
$type = $_POST['messageType']; 
$project = $_POST['projectName']; 
$firstName = $_POST['firstName']; 
$lastName = $_POST['lastName']; 
$message = $_POST['message']; 

$from = new SendGrid\Email($firstName . " " . $lastName, $email); 
$subject = $type == "Message" ? "www.teodorvecerdi.me | " . $type : "www.teodorvecerdi.me | " . $type . " | " . $project; 
$to = new SendGrid\Email("Teodor Vecerdi", "[email protected]"); 

$content2 = "<table><tr><td>Name</td><td>$firstName $lastName</td></tr><tr><td>Email</td><td>$email</td></tr>"; 
if($project != "I didn't select 'Suggestion for project'") 
    $content2 .= "<tr><td>Project</td><td>$project</td></tr>"; 
$content2 .= "<tr><td>Message</td><td>$message</td></tr></table>"; 

$content = new SendGrid\Content("text/html", $content2); 
$mail = new SendGrid\Mail($from, $subject, $to, $content); 

$apiKey = 'API KEY'; 
$sg = new \SendGrid($apiKey); 

$response = $sg->client->mail()->send()->post($mail); 
echo $response->statusCode(); 
print_r($response->headers()); 
echo $response->body();` 

そしてあるAJAX呼び出しが

$('#SendMessageForm').on("submit", function (e) { 
    e.preventDefault(); 
    var data = $(this).serialize(); 
    $.ajax({ 
     url: 'https://sendemail-teodorvecerdi.herokuapp.com', 
     type: 'POST', 
     data: data, 
     success: function (response) { 
      console.log(response); 
      console.log("Success"); 
     }, 
     fail: function (response) { 
      console.log(response); 
      console.log("Failure"); 
     } 
    }); 
}); 

です編集:私はこれは、ログに示したものですHerokuの

でスクリプトをホストしています私がフォームを提出するとき:

2017-08-[web.1]: [31-Aug-2017 22:22:06 UTC] PHP Fatal error: Uncaught Error: 
Call to undefined function SendGrid\mb_convert_encoding() in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php:745 
[web.1]: Stack trace: 
[web.1]: #0 /app/index.php(36): SendGrid\Content->__construct('text/html', '<table style='b...') 
[web.1]: #1 {main} [web.1]: thrown in /app/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php on line 745 
+0

PHPエラーログを確認しようとしましたか? – Neodan

+0

どこで見つけることができますか? –

+0

あなたのホスティング(設定)によって異なります – Neodan

答えて

0

私は最終的に問題を解決しました。私がしなければならなかったのは、作曲家のmbstringが@Neodanのようにできるようにすることでした。

関連する問題