2017-04-25 5 views
2

이메일을 보내기위한 간단한 sendgrid PHP 스크립트가 있습니다. 여기서 유일한 문제는 내가받는 사람을 더 추가해야한다는 것입니다. 따라서이 코드는 공식 문서를보고 있었지만 공식 문서를보고 있지만 할 수 없었습니다. 유용한 정보를 찾을 수있는 방법, 그리고 내가 여기에 더 많은받는 사람/이메일을 추가 할 필요가있는 사람을 알고 있습니다.Sendgrid php 여러명의 수신자에게 보내기

function sendEmail($subject, $to, $message) { 
    $from = new SendGrid\Email(null, "[email protected]"); 
    $subject = $subject; 

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

    $apiKey = 'MY_KEY'; 
    $sg = new \SendGrid($apiKey); 

    $response = $sg->client->mail()->send()->post($mail); 
    echo $response->statusCode(); 
} 
+1

전화 각 이메일 주소에 대한 기능. – muttonUp

+0

코드 샘플주세요. –

+1

정말요? 귀하의 주소를 반복하고 기능을 호출하십시오. – muttonUp

답변

3

SendGrid\Mail 클래스는 클래스 SendGrid\Personalization 통해 다수에게 to 주소를 추가 지원한다. 이메일의 봉투로 Personalizationhttps://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35

생각한다

당신은 여기에서 예를 볼 수 있습니다. 수신자의 주소 및 기타 유사한 데이터를 보유합니다. 각각 Sendgrid\Mail 개체는 하나 이상 Personalization이어야합니다. 당신이 사용하는 생성자를 통해

가하는 Personalization 객체가 이미 생성되어 여기를 참조 : https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958

당신은 Mail 객체 without this 나중에 add your ownPersonalization을 만들 수 있습니다.

+0

이렇게 해 주셔서 감사합니다. 아래 마지막 코드를 붙여 넣었습니다. –

+1

@ SuperMario'sYoshi 도움이된다면이 대답을 받아 들여야합니다. – ceejayoz

2

결국이 방법으로 문제를 해결할 수 있으며 제대로 작동합니다.

function sendEmail($subject, $to, $message, $cc) { 
$from = new SendGrid\Email(null, "[email protected]"); 
$subject = $subject; 

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

foreach ($cc as $value) { 
    $to = new SendGrid\Email(null, $value); 
    $mail->personalization[0]->addCC($to); 
} 

$apiKey = 'MY_KEY'; 
$sg = new \SendGrid($apiKey); 

$response = $sg->client->mail()->send()->post($mail); 
echo $response->statusCode(); 

}

1
function makeEmail($to_emails = array(),$from_email,$subject,$body) { 
    $from = new SendGrid\Email(null, $from_email); 

    $to = new SendGrid\Email(null, $to_emails[0]); 
    $content = new SendGrid\Content("text/plain", $body); 
    $mail = new SendGrid\Mail($from, $subject, $to, $content); 
    $to = new SendGrid\Email(null, $to_emails[1]); 
    $mail->personalization[0]->addTo($to); 

    return $mail; 
} 

function sendMail($to = array(),$from,$subject,$body) { 

    $apiKey = 'your api key'; 
    $sg = new \SendGrid($apiKey); 
    $request_body = makeEmail($to ,$from,$subject,$body); 
    $response = $sg->client->mail()->send()->post($request_body); 
    echo $response->statusCode(); 
    echo $response->body(); 
    print_r($response->headers()); 
} 

$to = array('[email protected]','[email protected]'); 
$from = '[email protected]'; 
$subject = "Test Email Subject"; 
$body = "Send Multiple Person"; 

sendMail($to ,$from,$subject,$body);