WP63
  • Web Development
  • WordPress Development
  • Plugins
  • WP63
  • Web Development

ส่งอีเมลด้วย SendGrid

Published April 4, 2022

ส่งอีเมลด้วย SendGrid
Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

การส่งอีเมลเป็นหนึ่งในสิ่งที่เราทำกันบ่อยๆ บนเว็บไซต์ ซึ่งท่าที่โปรแกรมเมอร์ PHP ชาวไทยใช้กันบ่อยๆ ก็คือเอา SMTP ของ Gmail มาใช่ส่งเมลผ่าน SMTP ด้วยไลบรารี่อย่าง PHPMailer

ปัญหาคืออีเมลที่ส่งออกไป มันจะเป็นแอดเดรสของ Gmail ที่เราเอา SMTP มาใช้ส่ง ซึ่งความน่าเชื่อถือมันต่ำมาก และในบางครั้งที่เราต้องส่งอีเมลจำนวนมาก ก็อาจจะติดลิมิตที่ 500 อีเมลต่อวัน และไม่สามารถขยายเพิ่มได้ตามความต้องการ (เว้นแต่จะขยันมานั่งเปลี่ยน SMTP เองทุกครั้งที่มันเต็ม)

ทางออก (และควรเป็นวิธีแรกๆ ที่เลือกใช้กัน) คือการหันมาใช้บริการ transaction email อย่างเช่น Mailgun, Mailchimp Transaction Email, หรืออย่าง SendGrid ที่เราจะเขียนถึงในตอนนี้

  • ตั้งค่า Mailgun บนเซิร์ฟเวอร์ Cloudways สำหรับใช้ส่งอีเมลออกจากเว็บ

ข้อดีของการส่งผ่านบริการ transaction email มันก็มีหลายอย่าง เช่น

  • ตั้งอีเมลที่ส่งเป็นโดเมนของเราได้ เพิ่มความน่าเชื่อถือได้เยอะมาก
  • ปกติแล้วจะส่งเมลเข้า inbox แน่นอน
  • ถ้าต้องส่งจำนวนมาก สามารถใช้เงินแก้ปัญหา จ่ายเพื่อเพิ่มโควต้าส่งเมลได้
  • ส่วนใหญ่จะมีให้เลือกทั้งการส่งผ่าน SMTP แบบเดิม (เอา SMTP, Username, และ Password ไปกรอกเหมือนที่ทำกันทุกวันนี้) หรือส่งผ่าน API เฉพาะก็ได้เช่นกัน
  • ปกติบริการพวกนี้จะมีบริการด้าน marketing ต่างๆ ให้ด้วย (แน่นอนว่าเสียเงิน) เช่นการยืนยันว่าเมลปลายทางมีตัวตนจริงหรือไม่ การติดตามว่าอีเมลส่งถึงผู้รับจริงหรือไม่ รวมกับการ tracking อื่นๆ

สำหรับ SendGrid นั้น แพลนฟรีจะจำกัดการส่งไว้ที่ 100 ฉบับต่อวัน หรือถ้าใช้เยอะกว่านี้มาก ก็สามารถสมัครใช้แพลน Essential 40k ได้ที่ $14.95 ต่อเดือน จะได้โควต้าเพิ่มขึ้นมาเป็น 40,000 ฉบับต่อเดือน

การยืนยันโดเมนและตั้งค่า DNS

ขอข้ามขั้นตอนการสมัครสมาชิกมาที่ขั้นตอนการตั้งค่าเลยแล้วกันนะ

SendGrid จะมีวิธียืนยันตัวตอนสองแบบ คือแบบ Single sender ที่จะส่งเมลมาให้เรายืนยันตัวตน วิธีนี้อีเมลที่ใช้ส่งจะขึ้นเป็น <email> via sendgrid.net เช่น [email protected] via sendgrid.net

อีกวิธีคือ Domain verification วิธีนี้จะทำให้เราสามารถส่งอีเมลในชื่อโดเมนของเราได้โดยตรง โดยไม่มี via sendgrid.net ต่อท้าย ซึ่งเราจะพามาดูวิธีนี้กัน

ขั้นแรก พอเราสมัครสมาชิกเสร็จและล็อกอินเข้ามาครั้งแรก จะเจอแบนเนอร์ให้เราเซ็ตอัพ Sender identity ให้เรากดเลือก Authenticate domain instead

ขั้นตอนต่อมาให้เราเลือกว่าเราใช้ DNS Provider เจ้าไหน อย่างผมใช้ Cloudflare ก็เลือก Cloudflare ขึ้นมา จริงๆ ตรงนี้เลือกอะไรไม่ค่อยสำคัญเท่าไหร่ มันจะแค่เลือกดึงคู่มือออกมาได้ถูกว่า provider เจ้าไหน เข้าไปแก้ตรงไหน (ถ้ามีใครยังใช้ตัวจัดการ DNS ใน DirectAdmin อยู่ ก็เข้าไปแก้ DNS Record ใน DA ได้เลย)

อีกตัวเลือกคือเป็น branded tracking คือพวกลิงก์ tracking ต่างๆ ค่าเริ่มต้นจะเป็นลิงก์ไปหา sendgrid.net การเลือก Yes ตรงนี้จะทำให้เราสามารถเปลี่ยน tracking link ไปเป็นโดเมนที่เราเลือกเองได้

พอกด Next แล้วจะให้เรากรอกโดเมนของเรา ก็ใส่โดเมนเว็บเราแล้วกด Next ไปได้เลย

จากนั้นในหน้าถัดไป เขาจะให้ DNS Record เรามาสามชุด

ถึงตรงนี้ให้เราไปที่ DNS Provider ที่เราใช้ แล้วกรอก CNAME Record ตามค่าที่เขาให้มาให้ครบทั้งสามรายการ เสร็จแล้วให้ติ๊กถูกที่ I’ve added these records แล้วกด Verify ได้เลย

ถึงตรงนี้ถ้าเรายืนยันโดเมนผ่าน ก็จะถือว่าเสร็จสมบูรณ์ พร้อมสำหรับการส่งอีเมลแล้ว

ส่งอีเมลผ่าน SMTP

มาเริ่มกันที่ท่ามาตรฐานกันก่อน นั่นคือการส่งผ่าน SMTP วิธีนี้เหมาะกับคนที่จะเปลี่ยนจาก Gmail มาใช้ SendGrid เพราะเราไม่ต้องเขียนโค้ดใหม่ แค่แก้ค่า SMTP ก็สามารถใช้ได้เลย

ให้เราไปที่เมนู Email API > Integration Guide แล้วเลือก SMTP Relay

หน้าถัดไปให้เราตั้งชื่อ API Key แล้วกด Create key จากนั้นไปแก้ค่า SMTP ให้เรียบร้อย

Server: smtp.sendgrid.net
Port: 25 หรือ 587 สำหรับส่งแบบ no-ssl หรือ 465 สำหรับส่งผ่าน ssl
Username: apikey
Password: API Key ที่กดสร้างมาเมื่อสักครู่

จากนั้นกด I’ve updated my settings แล้วกด Verify Integration ได้เลย เท่านี้ก็พร้อมใช้งาน

ขั้นตอนนี้เราก็แค่ลองให้เว็บแอพของเรามันส่งอีเมลดูว่าส่งได้หรือเปล่า ถ้าส่งได้ก็กลับมากด Verify Integration ก็เป็นอันเสร็จสมบูรณ์

ส่งอีเมลผ่าน Web API

พูดกันสั้นๆ ก็คือการส่งอีเมลผ่าน Web API นั้นเร็วกว่าและประหยัดแบนด์วิดกว่าการส่งด้วย SMTP เพราะเซิร์ฟเวอร์เว็บเราไม่ต้องคุยกับเซิร์ฟเวอร์ SMTP หลายครั้ง เรายิงแพ็คเก็ตไปหา API แล้วรอ API ตอบสถานะกลับมาอย่างเดียว จบ

การส่งผ่าน Web API จะมีขั้นตอนเพิ่มขึ้นมาเล็กน้อย วิธีนี้จะเหมาะกับคนที่เริ่มสร้างเว็บแอพใหม่ตั้งแต่ต้น ซึ่งทาง SendGrid ก็เตรียม Library เอาไว้ให้เลือกหลายภาษามาก ทั้ง Node.js, Python, C#, Go, cURL และแน่นอนรวมถึง PHP ด้วย

ขั้นแรกให้เราสร้างไฟล์ sendgrid.env เอาไว้ที่ root directory ของเว็บเรา ในไฟล์ให้ใส่เป็นดังนี้

export SENDGRID_API_KEY='YOUR_API_KEY'

และอย่าลืมเพิ่ม sendgrid.env เข้าไปใน .gitignore ด้วยล่ะ

จากนั้นให้เราติดตั้งแพ็คเกจ sendgrid/sendgrid ลงในโปรเจ็กท์ของเราผ่าน composer

composer require sendgrid/sendgrid

หรือถ้าไม่ได้ใช้ composer ก็ไปดาวน์โหลดแพ็คเกจเวอร์ชันล่าสุดมาจาก GitHub แล้ว require ไฟล์ sendgrid-php.php เข้ามา ก็สามารถเริ่มใช้งานได้ทันที

  • Sendgrid-php GitHub

วิธีส่งก็ง่ายๆ ตาม example code ที่เขาเตรียมไว้ให้ได้เลย

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("[email protected]", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("[email protected]", "Example User");
$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(getenv('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";
}

ส่วนใครที่ใช้ภาษาอื่นที่ไม่มีไลบรารี่ของ SendGrid ก็สามารถยิงผ่าน API โดยตรงได้เลย

  • API Documentation

Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

บทความอื่นๆ ที่น่าสนใจ

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2022 WP63

  • หน้าแรก
  • ติดต่อเรา
  • นโยบายความเป็นส่วนตัว
  • ข้อตกลงการใช้งาน
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
  • Web Development
  • WordPress Development
  • Plugins

Like us

Like us

Categories

  • Blog
  • Gutenberg
  • Plugins
  • Shortnotes
  • Snippets
  • Web Development
  • WordPress Development

Popular Posts

  • ส่งอีเมลด้วย SendGrid
    ส่งอีเมลด้วย SendGrid
  • array_map() ใน PHP
    array_map() ใน PHP
  • กำหนด URL structure ได้ตามใจด้วย AltoRouter
    กำหนด URL structure ได้ตามใจด้วย AltoRouter
  • การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
    การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
  • การทำ Routing ใน PHP ด้วย AltoRouter
    การทำ Routing ใน PHP ด้วย AltoRouter
  • ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ
    ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ

Archives

  • April 2022
  • January 2021
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • March 2019
  • February 2019
  • December 2018
  • September 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018