SendGrid API Example

Doc: https://github.com/sendgrid/sendgrid-php

Complete example:
https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#attachments

Example:

   public function send($subject, $msg, $contact, $attachment=null){
        $from = new SendGrid\Email($this->sender['name'], $this->sender['email']);
        $subject = $subject;
        $to = new SendGrid\Email($contact['name'], $contact['email']);
        $content = new SendGrid\Content("text/html", $msg);
        $mail = new SendGrid\Mail($from, $subject, $to, $content);

        if($attachment != false){
            $att = $this->process_attachment($attachment);
            $mail->addAttachment($att);
        }

        $apiKey = $this->sg_key;
        $sg = new \SendGrid($apiKey);

        $response = $sg->client->mail()->send()->post($mail);
        if($response->statusCode() == 202){
            return true;
        }else{
            $this->err = '寄送Email失敗: ' . $contact['email'];
            return false;
        }


        echo '<prev>';
        echo $response->statusCode();
        print_r($response->headers());
        echo $response->body();
        echo '</prev>';
    }

    public function process_attachment($file){
        $file_encoded = base64_encode(file_get_contents($file));
        $attachment = new SendGrid\Attachment();
        $attachment->setContent($file_encoded);
        $attachment->setType("application/pdf");
        $attachment->setDisposition("attachment");
        $attachment->setFilename("order.pdf");

        return $attachment;
    }