2017-05-07 116 views
1

我想使用SendGrid SMTP服務從我的網站的聯繫表格發送電子郵件。表單上的提交按鈕只是吐出我的網站的空白版本,沒有電子郵件發送到我的收件箱。SendGrid SMTP與聯繫表格集成

這裏是PHP代碼:

<?php 

require("class.phpmailer.php"); 
    $mail = new PHPMailer(); 

    // SMTP & Sendgrid settings 
    $mail->IsSMTP(); 
    $mail->Host = "smtp.sendgrid.net"; 
    $mail->Port = "465"; 
    $mail->SMTPAuth = "true"; // Enable SMTP authentication 
    $mail->Username = "sendgrid username"; 
    $mail->Password = "sendgrid password"; 
    $mail->SMTPSecure = ''; // Enable encryption, 'ssl' also accepted 

    // Email headers and body 
    $mail->SetFrom("[email protected]"); 
    $mail->AddAddress("[email protected]"); 

    // Form fields 
     $Name = $_POST['Name']; 
     $Email = $_POST['Email']; 
     $Contact = $_POST['Contact']; 
     $Message = $_POST['Message']; 


    $mail->Subject = "Message from yoursite.com"; 
    $mail->Body  = "You have a new message from your contact form on site.com \n \n Name: $Name \n Email: $Email \n Contact: $Contact \n Message: $Message"; 
    $mail->WordWrap = 50; 


    if(!$mail->Send()) { 
     echo 'Message was not sent.'; 
     echo 'Mailer error: ' . $mail->ErrorInfo; 
    } 
    else { 
     echo 'Message has been sent.'; 
    } 
header('Location: mywebsite.com'); 
?> 
+1

請看這裏,require(「class.phpmailer.php」); $ mail = new PHPMailer();',如果你想使用SendGrid API,那你爲什麼在這裏使用PHPMailer庫?閱讀這裏的文檔,[https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html](https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html) –

+0

噢好吧。所以我需要使用SendGrids PHP庫。我已經下載了庫,將sendgrid-php.php文件複製到我的根目錄,並將以下代碼行添加到我的php文件中: require(「sendgrid-php.php」); 這是你在說什麼? – kfm1607

+1

是的。最重要的是,您還必須重構代碼,因爲沒有任何PHPMailer相關的代碼可以與SendGrid API一起使用。 –

回答

1

這裏是一步一步的進程發送使用SendGrid API電子郵件,

  • Download Packaged Library下載存檔SendGrid庫。解壓縮並將其放入項目目錄中。
  • 轉到https://app.sendgrid.com/settings/api_keys併爲您的帳戶/應用程序創建一個API密鑰。
  • 現在來編碼部分。重構代碼以下列方式,

    require_once('sendgrid-php/sendgrid-php.php'); 
    
    $from = new SendGrid\Email(null, "SENDER'S_EMAIL_ADDRESS"); 
    $subject = "Hello World from the SendGrid PHP Library!"; 
    $to = new SendGrid\Email(null, "RECEIVER'S EMAIL ADDRESS"); 
    $content = new SendGrid\Content("text/plain", "Hello, Email!"); 
    $mail = new SendGrid\Mail($from, $subject, $to, $content); 
    
    $apiKey = 'YOUR_API_KEY'; 
    $sg = new \SendGrid($apiKey); 
    
    $response = $sg->client->mail()->send()->post($mail); 
    if($response->statusCode() == 202){ 
        echo "Email sent successfully"; 
    }else{ 
        echo "Email could not be sent"; 
    } 
    

    不要忘記更改SENDER'S_EMAIL_ADDRESSRECEIVER'S_EMAIL_ADDRESSYOUR_API_KEY按您的要求。最重要的是,根據您的應用程序的要求更改電子郵件的主題和正文。


旁註:萬一有什麼差錯在中途,使用以下三種響應方式進行調試。

echo $response->statusCode(); 
var_dump($response->headers()); 
echo $response->body(); 
+0

非常感謝您的幫助!有效!!電子郵件發送成功並收到! 請最後一個問題。我希望從聯繫表單中提取電子郵件的內容。我怎麼做?例如,我想要$ from = new SendGrid \ Email(null,「SENDER'S_EMAIL_ADDRESS」);從表單中提取電子郵件的值。 – kfm1607

+0

@Anonymous只需將變量代替'SENDER'S_EMAIL_ADDRESS',就是這樣。這裏有一個例子,'$ from = new SendGrid \ Email(null,$ Email);'。另外,如果解決了您的問題,請*接受*答案。 [如何接受Stack Overflow的答案?](https://meta.stackexchange.com/a/5235) –

+0

接受。非常感謝:) – kfm1607