因為用heroku的mailgun , 明明該設定好的 ; 都設定了
在遠振主機上面 , 發信都很正常 ; 但是到了heroku就是不行...
(我在heroku的表單 , 都收不到信...)
所以我今天換了另一個郵件外掛 - sendgrid
使用起來也蠻方便的 ~
但有個地方要特別注一下
官網教學 https://app.sendgrid.com/guide/integrate/langs/php
基本上就是照官網的教學 , 一步一步來就OK了
source ./sendgrid.env 這個用不起來 , 不要管他
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); 這一行的getenv要拿掉
直接打API_KEY就好 , 所以就是
$sendgrid = new \SendGrid('SENDGRID_API_KEY')
-----
附上本機測試成功的程式碼
# 使用Twilio SendGrid插件來寄信
require 'vendor/autoload.php';
require 'vendor/sendgrid/sendgrid/sendgrid-php.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("n95230@gmail.com","n95230");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("n95230@gmail.com","tony");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid('這邊直接打API_KEY即可');
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
-----
heroku雲端程式碼
//抓取表單各個值
$name=$_POST['name'];
$emailA=$_POST['email'];
$textarea=$_POST['textarea'];
# 使用Twilio SendGrid插件來寄信
require 'vendor/autoload.php';
require 'vendor/sendgrid/sendgrid/sendgrid-php.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("n95230@gmail.com","heroku網站表單"); //寄件人信箱一定要驗證過 , 不然會發信失敗 然後信箱和驗證人的信箱要相同
$email->setSubject("網站表單內容");
$email->addTo("n95230@gmail.com","湯尼");
$email->addContent("text/html",'姓名:'.$name.'<br>'.'電子信箱:'.$emailA.'<br>'.'意見:'.$textarea); //抓取表單內容ㄝ,寄到信箱裡面去
$sendgrid = new \SendGrid('API_KEY'); //API_KEY
$response = $sendgrid->send($email);
//確認有無寄信成功
if($response)
{
echo "寄信成功 , 5秒後轉回首頁";
header("Refresh:5; url=index.php");
}
else
{
echo "寄信失敗"."<br>";
echo $mail->ErrorInfo;
}
參考資料
1. https://stackoverflow.com/questions/61149142/node-js-sendgrid-mail-403-forbidden-error
2. https://stackoverflow.com/questions/63576851/sendgrid-unauthorized-wrong-credentials-php
3. https://www.youtube.com/watch?v=fEobqi3N7zw
4. https://www.courier.com/error-solutions/http-error-403-forbidden-sendgrid/
5. https://adersaytech.com/wordpress-teach/cloudflare-registrar-domain.html
今天把網路中文的DNS , 指到cloudflare了 ; 改TXT那些的速度真的快超多 ...
幾分鐘或幾十分鐘就好了 , 不用到幾小時
可惜idv.tw網域 , 不能夠轉移到cloudflare 😑
網路中文算不錯 , 可是DNS那些改的時間 ; 太久了 ....