Sendgridのサンプルを使って送信環境を作っています。
https://sendgrid.kke.co.jp/blog/?p=1066
SESSIONの渡し方はこちら。
http://www.kaasan.info/archives/2151
以下のように、3つのファイルがあり、index.htmlからconfirm.phpへのデータの受け渡しはうまく行き、画面にはHTMLで表示されています。
index.html
html
1<!doctype html> 2<html> 3<head> 4 省略 5</head> 6<body> 7 〜 8<form class="ui form" id="form" name="form" action="confirm.php" method="post"> 9<div class="field"> 10 <label>お名前</label> 11 <input id="fullname" type="text" name="fullname" placeholder="例:山田 太郎"> 12</div> 13<button type="submit" value="確認"></button> 14</form> 15</body> 16</html>
confirm.php
php
1<? php 2if(!$_POST) { 3header('Location: ./index.html'); 4session_start(); 5$_SESSION["fullname"] = $_POST["fullname"]; 6}; 7?> 8<!doctype html> 9<html> 10<head> 11 省略 12</head> 13<body> 14 〜 15 <form id="form" action="sendmail.php" method="post"> 16 <p>お名前:<?php echo htmlspecialchars($_POST["fullname"]); ?></p> 17 <button type="submit"> 18 <img src="images/submit.png" class="submit-btn"> 19 </button> 20 </form> 21</body> 22</html>
ところが、
sendmail.php
php
1<?php 2session_start(); 3if(!$_SESSION=["fullname"]) { 4header('Location: ./index.html'); 5}; 6 7var_dump($_SESSION); 8 9require 'vendor/autoload.php'; 10$dotenv = new Dotenv\Dotenv(__DIR__); 11$dotenv->load(); 12 13$api_key = $_ENV['API_KEY']; <--独自の環境変数から読み込み 14$from = $_ENV['FROM']; <--独自の環境変数から読み込み 15$tos = explode(',', $_ENV['TOS']); <--独自の環境変数から読み込み 16 17$fullname = $_SESSION["fullname"]; <-- 20行目 18 19省略 20 21$sendgrid = new SendGrid($api_key, array("turn_off_ssl_verification" => true)); 22$email = new SendGrid\Email(); 23$email->setSmtpapiTos($tos)-> 24 setFrom($from)-> 25 setFromName("送信者名")-> 26 setSubject("[申し込みが来ました] %fullname%さんから申し込みが来ました")-> 27 setText("%fullname%さん \r\n ")-> 28 setSubstitution('%fullname%', '$fullname')-> <--43行目 29 30省略 31?> 32
と書きました。そしたら、
php
1array(1) { [0]=> string(8) "fullname" } 2Notice: Undefined index: fullname in /var/www/html/lp/sendmail.php on line 20 3 4Fatal error: Uncaught Error: Call to undefined method SendGrid\Email::setSubstitution() in /var/www/html/lp/sendmail.php:43 Stack trace: #0 {main} thrown in /var/www/html/lp/sendmail.php on line 43 5 6Notice: Unknown: Skipping numeric key 0 in Unknown on line 0
とエラーを吐きました。画面上はデータが表示されていましたが内部的にデータを保持できておらず、fullnameに上書きされてしましました。これをデータをきちんと渡せれば解決するとおもうので、どうしたらデータを引き継げるのか教えてください。
ーーーーー
sendmail.php
php
1<?php 2session_start(); 3if(!$_SESSION['fullname']=="") { 4header('Location: ./index.html'); 5}; 6 7省略 8$sendgrid = new SendGrid($api_key, array("turn_off_ssl_verification" => true)); 9$email = new SendGridEmail();
エラーメッセージ
php
1Notice: Undefined index: fullname in /var/www/html/lp/sendmail.php on line 7 2array(0) { } 3Notice: Undefined index: fullname in /var/www/html/lp/sendmail.php on line 20 4 5Fatal error: Uncaught Error: Class 'SendGridEmail' not found in /var/www/html/lp/sendmail.php:37 Stack trace: #0 {main} thrown in /var/www/html/lp/sendmail.php on line 37
対応した結果、値が渡っていないという現象は改善してないようです。
回答2件
あなたの回答
tips
プレビュー