Php ส ง email ต องม mail server ม ย

SMTP จะมีชุดคำสั่งที่ค่อนข้างง่ายสำหรับใช้สื่อสารหรือส่ง email ระหว่าง mail server ทำงานโดยการให้ Server แยกส่วนของข้อมูลออกมาเป็นหมวดหมู่ ที่ server ปลายทางสามารถเข้าใจได้และเมื่อส่ง mail ออกไป ข้อมูลในรูป string หรือ text จะถูก แยกออกมาเป็นส่วนๆเพื่อวิเคราะห์หาสิ่งที่ต้องทำในส่วนนั้นๆ

SMTP จะช่วยในเรื่อง code ในการจำแนกข้อมูล message ใน mail server ออกแบบมาให้ทำความเข้าใจความหมายของข้อมูลเหล่านั้น ขณะที่ message ถูกส่งผ่านไปยังปลายทาง บางครั้งอาจจะต้องผ่าน computer จำนวนมากซึ่งทำงานโดยการ stored and forward ไปยัง computer ลำดับต่อไปในเส้นทางนั้นเรื่อยๆ ก็เหมือนจดหมายนั้นถูกส่งต่อผ่านมือแต่ละคนระหว่างทางไปจนถึง mailbox

กระวบวนทำงาน SMTP Protocol

เปรียบเหมือนการใช้งานโทรศัพท์ คือ หมายเลขโทรศัพท์ = IP , เครือข่าย = SMTP , ข้อความ = อีเมล ซึ่งโดยปกติแล้ว SMTP จะประกอบด้วย E-mail, Username, Password, Mail Sever

ตัวอย่าง SMTP Transport

โดยปกติตัวอย่างการส่ง message ผ่าน SMTP ไปยัง 2 mailbox หรือ alice กับ theboss ซึ่งทั้งคู่อยู่ใน domain เดียวกันคือ .com ขั้นตอนการทำงานจะเป็นดังนี้

  1. สร้าง connection จาก sender ไปยัง SMTP server ซึ่งหลังจาก establish connection แล้วจะได้รับ greeting message จากฝั่ง server
  2. ฝั่ง client จะต้องส่ง command HELO เพื่อยืนยันตัวตน และฝั่ง server ก็จะตอบรับกลับมาถ้า ข้อมูลถูกต้อง (250 OK)
  3. ส่งข้อมูล Mail from ว่าส่งจากใคร
  4. ส่งข้อมูล RCPT TO ว่าส่งใคร ซึ่งในที่นี้มี 2 mailbox ก็คือ 2 command
  5. ส่งข้อมูล body ของ mail โดยเริ่มต้นด้วย DATA command และจบด้วย Dot(.)
  6. หลังจากส่ง Body mail แล้ว ปิด Connection ด้วยการส่ง QUIT command หรือ ถ้าไม่ส่ง ก็มีจะ timeout อยู่ 10 นาที

ตัวอย่าง

ตัวอย่าง SMTP Transport

SMTP ก็มาตรฐานบน internet สำหรับการรับส่ง E-mail นั้นเอง หรือจะเรียกว่า Protocol ส่ง mail ก็ว่าได้ ปัจจุบัน mail server และ ระบบส่งเมล์ทั้วโลก ต่างใช้ smtp ในการรับและส่งข้อมูล mail กันหมดแล้ว ซึ่งทางฝั่ง Client เองตัว Program mail จะใช้ SMTP สำหรับขาส่งไปหา mail server เท่านั้น สำหรับขารับ Client program จะใช้ IMAP หรือ POP3 และSMTP จะสื่อสารกันระหว่าง mail server ด้วย PORT 25 ในทางกลับกันฝั่ง mail client จะส่ง mail ไปยัง mail server ที่ port 587 แต่บางครั้งก็อาจจะไปใช้ port 456 แทนด้วยเหตุผลบางประการ และสำหรับ SSL connection จะถูกเรียกว่า protocol ว่า SMTPS

I know this is an old question but it's still active and all the answers I saw showed basic authentication, which is deprecated. Here is an example showing how to send via Google's Gmail servers using PHPMailer with XOAUTH2 authentication: //Import PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\OAuth; //Alias the League Google OAuth2 provider class use League\OAuth2\Client\Provider\Google; //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('Etc/UTC'); //Load dependencies from composer //If this causes an error, run 'composer install' require '../vendor/autoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer(); //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging //SMTP::DEBUG_OFF = off (for production use) //SMTP::DEBUG_CLIENT = client messages //SMTP::DEBUG_SERVER = client and server messages $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; //Set the SMTP port number: // - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or // - 587 for SMTP+STARTTLS $mail->Port = 465; //Set the encryption mechanism to use: // - SMTPS (implicit TLS on port 465) or // - STARTTLS (explicit TLS on port 587) $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Set AuthType to use XOAUTH2 $mail->AuthType = 'XOAUTH2'; //Fill in authentication details here //Either the gmail account owner, or the user that gave consent $email = '[email protected]'; $clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com'; $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP'; //Obtained by configuring and running get_oauth_token.php //after setting up an app in Google Developer Console. $refreshToken = 'RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0'; //Create a new OAuth2 provider instance $provider = new Google( [ 'clientId' => $clientId, 'clientSecret' => $clientSecret, ] ); //Pass the OAuth provider instance to PHPMailer $mail->setOAuth( new OAuth( [ 'provider' => $provider, 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'refreshToken' => $refreshToken, 'userName' => $email, ] ) ); //Set who the message is to be sent from //For gmail, this generally needs to be the same as the user you logged in as $mail->setFrom($email, 'First Last'); //Set who the message is to be sent to $mail->addAddress('[email protected]', 'John Doe'); //Set the subject line $mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->CharSet = PHPMailer::CHARSET_UTF8; $mail->msgHTML(file_get_contents('contentsutf8.html'), __DIR__); //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file $mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message sent!'; }

Reference: PHPMailer examples folder

Toplist

โพสต์ล่าสุด

แท็ก

ไทยแปลอังกฤษ แปลภาษาไทย โปรแกรม-แปล-ภาษา-อังกฤษ พร้อม-คำ-อ่าน ห่อหมกฮวกไปฝากป้าmv Terjemahan แปลภาษาอังกฤษเป็นไทย pantip lmyour แปลภาษา ไทยแปลอังกฤษ ประโยค แอพแปลภาษาอาหรับเป็นไทย เมอร์ซี่ อาร์สยาม ล่าสุด แปลภาษาอาหรับ-ไทย Bahasa Thailand app แปลภาษาไทยเป็นเวียดนาม พจนานุกรมศัพท์ทหาร ยศทหารบก ภาษาอังกฤษ สหกรณ์ออมทรัพย์กรมส่งเสริมการปกครอง ส่วนท้องถิ่น แปลภาษาเวียดนามเป็นไทยทั้งประโยค กรมส่งเสริมการปกครองท้องถิ่น การไฟฟ้าส่วนภูมิภาคมีทั้งหมดกี่ภาค มัจจุราชไร้เงา 1 mono29 มัจจุราชไร้เงา 1 pantip มัจจุราชไร้เงา 3 pantip รายชื่อวิทยานิพนธ์ นิติศาสตร์ 2563 ศัพท์ทหาร ภาษาอังกฤษ pdf ห่อหมกฮวกไปฝากป้า หนังเต็มเรื่อง แปลภาษาอิสลามเป็นไทย ่้แปลภาษา Google Drive กรมการปกครอง กระบวนการบริหารทรัพยากรมนุษย์ 8 ขั้นตอน การไฟฟ้าส่วนภูมิภาค ข้อสอบคณิตศาสตร์ พร้อมเฉลย คะแนน o-net โรงเรียน ที่อยู่สมุทรปราการ ภาษาอังกฤษ ประปาไม่ไหล วันนี้ มหาวิทยาลัยรามคําแหง เปิดรับสมัคร 2566 มัจจุราชไร้เงา 2 facebook ราคาปาเจโร่มือสอง สถาบันพัฒนาบุคลากรท้องถิ่น หนังสือราชการ ส ถ หยน ห่อหมกฮวกไปฝากป้า คาราโอเกะ อาจารย์ ตจต Google Form Info arifureta shokugyou de sekai saikyou manga online legendary moonlight sculptor www.niets.or.th ประกาศผลสอบ การบริหารทรัพยากรมนุษย์ มีอะไรบ้าง ข้อสอบภาษาอังกฤษ พร้อมเฉลย pdf