
- 環境 ConoHa
- OS: CentOS7
APIのキーを取得しよう

【Settings】→【APIKeys】を選択します。

【Creatte API Key】をクリックします。

【Cretate & View】をクリックします。

キーをメモして控えておく。
APIで利用してみよう CentOS7
# yum -y install git php httpd
systemctl restart httpd systemctl enable httpd systemctl status httpd
Composerのインストール
# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# php composer-setup.php
# php -r "unlink('composer-setup.php');"
# mv composer.phar /usr/local/bin/composer
# cd /var/www/html # git clone https://github.com/sendgridjp/sendgridjp-php-example.git # cd sendgridjp-php-example # cp .env.example .env
# vi .env API_KEY=your_api_key TOS=you@youremail.com,friend1@friendemail.com,friend2@friendemail.com FROM=you@youremail.com ↓変更 API_KEY=SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxE TOS=kanehiro@example.net,example@gmail.com,yuu@example.tokyo FROM=yuu@example.net
Composerでインストール
# composer install
サンプルプラグラム確認
# cat sendgrid-php-example.php
<?php
require 'vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$api_key           = $_ENV['API_KEY'];
$from              = $_ENV['FROM'];
$tos               = explode(',', $_ENV['TOS']);
$sendgrid = new SendGrid($api_key, array("turn_off_ssl_verification" => true));
$email    = new SendGrid\Email();
$email->setSmtpapiTos($tos)->
       setFrom($from)->
       setFromName("送信者名")->
       setSubject("[sendgrid-php-example] フクロウのお名前は%fullname%さん")->
       setText("%familyname% さんは何をしていますか?\r\n 彼は%place%にいます。")->
       setHtml("<strong> %familyname% さんは何をしていますか?</strong><br />彼は%place%にいます。")->
       addSubstitution("%fullname%", array("田中 太郎", "佐藤 次郎", "鈴木 三郎"))->
       addSubstitution("%familyname%", array("田中", "佐藤", "鈴木"))->
       addSubstitution("%place%", array("%office%", "%home%", "%office%"))->
       addSection('%office%', '中野')->
       addSection('%home%', '目黒')->
       addCategory('category1')->
       addHeader('X-Sent-Using', 'SendGrid-API')->
       addAttachment('./gif.gif', 'owl.gif');
$response = $sendgrid->send($email);
var_dump($response);
実行
http://IPアドレス/sendgridjp-php-example/sendgrid-php-example.php

ソースの確認

Fromのドメインが一致していないので、次はDNSの設定も行いましょうか。












