2017-08-31 25 views
0

我有一個使用SendGrid發送電子郵件的PHP腳本。如果我擺脫$ _POST命令,請手動設置值並在本地運行(從cmd)它的工作。但是當試圖使用Ajax提交表單時,我得到一個500內部服務器錯誤。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所說的那樣。

相關問題