2016-04-13 60 views
0

我在表單中使用SendGrid Api和PHP cURL。我的目標是將數據發送到網站的管理員,併發送確認電子郵件給已填寫表格的用戶。如何使用SendGrid和PHP拍攝兩封電子郵件(管理員和填寫表單的用戶)

我已經成功實施了電子郵件到管理員部分,但如何向用戶同時拍攝另一封電子郵件?請幫忙。

<?php 
$url = 'https://api.sendgrid.com/'; 
$user = 'xyz'; 
$pass = 'mypass'; 


$params = array(
    'api_user' => "$user", 
    'api_key' => "$pass", 
    'to'  => "[email protected]", 
    'subject' => "EM Workshop Registration", 
    'html'  => "<html><head><body> message and data goes here </body></head></html> 
'from'  => "[email protected]", 
); 

$request = $url.'api/mail.send.json'; 

// Generate curl request 
$session = curl_init($request); 
// Tell PHP not to use SSLv3 (instead opting for TLS) 
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); 
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey)); 
// Tell curl to use HTTP POST 
curl_setopt ($session, CURLOPT_POST, true); 
// Tell curl that this is the body of the POST 
curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
// Tell curl not to return headers, but do return the response 
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

// obtain response 
$response = curl_exec($session); 
curl_close($session); 


header('Location: thanks.html'); 
exit(); 

print_r($response); 

?> 

回答

1

可以使用bcc參數將電子郵件發送給用戶太多,只是修改$params作爲

$params = array(
    'api_user' => "$user", 
    'api_key' => "$pass", 
    'to'  => "[email protected]", 

    'subject' => "EM Workshop Registration", 
    'html'  => "<html><head><body> message and data goes here </body></head></html> 
    'from'  => "[email protected]", 
    'bcc'  => "[email protected]" //JUST ADD THIS LINE 
); 
+0

感謝AYUSH, 但實際上我想送2組不同的電子郵件。 例如, 管理員將獲得表單數據,用戶將得到一個消息,如感謝您提交.​​.. –

+0

您可以創建一個2文件的功能,一個用戶和一個管理員用不同的內容,然後從你的php文件的主體調用這些函數 –

相關問題