欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)收發(fā)郵件的方法詳解

 更新時(shí)間:2023年11月01日 16:43:25   作者:AreoWarm  
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)收發(fā)郵件的多種方法總結(jié),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解下

1、添加擴(kuò)展:

# 第一種:
composer require php-imap/php-imap
# 第二種:
composer require phpmailer/phpmailer

2、這里采用第二種方式:

<?php
declare(strict_types=1);

namespace App\Controller\v1\email;


use App\Controller\AbstractController;
use PhpImap\Exception;
use PhpImap\Mailbox;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

/**
 * Desc: 電子郵件相關(guān) 
 */
class EmailController extends AbstractController
{

    public const imapServer = '{imap.qq.com:993/imap/ssl}'; // QQ 郵箱的 IMAP 服務(wù)器地址
    public const imapUsername = '11***039@qq.com'; // 您的 QQ 郵箱地址
    public const imapPassword = '***';  // 您的 QQ 郵箱密碼
    public const attachmentPath = BASE_PATH . '/storage/email/'; // 您的 附件 保存目錄


    /**
     * Desc: 方法一 郵件發(fā)送與接收 -- 如果亂碼需要配置 GB2312  UTF-8 
     * Date: 2023-10-31 18:38
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function receiveMailWithAttachments()
    {
        $params = $this->request->post();
        $imapServer = $params['imapServer'] ?? self::imapServer; // QQ 郵箱的 IMAP 服務(wù)器地址
        $smtpServer = $params['smtpServer'] ?? self::imapServer; // QQ 郵箱的 smtp 服務(wù)器地址
        $imapUsername = $params['imapUsername'] ?? self::imapUsername; // 您的 QQ 郵箱地址
        $imapPassword = $params['imapPassword'] ?? self::imapPassword; // 您的 QQ 郵箱密碼
        var_dump($imapServer, $smtpServer, $imapUsername, $imapPassword);
        $mail = new PHPMailer(true);
        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host = $smtpServer;//'smtp.example.com';                     //Set the SMTP server to send through
            $mail->SMTPAuth = true;                                   //Enable SMTP authentication
            $mail->Username = $imapUsername;//'user@example.com';                     //SMTP username
            $mail->Password = $imapPassword;                               //SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
            $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //測(cè)發(fā)送 -- 成功
            $mail->setFrom($imapUsername, 'Mailer');
            $mail->addAddress('28***511@qq.com', 'John Doe');
            $mail->Subject = 'PHPMailer SMTP test';
            $mail->Body = 'This is a test email message';
            if (!$mail->send()) {
                echo 'Message could not be sent.' . "\n";
                echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
            } else {
                echo 'Message has been sent' . "\n";
            }

            $msg_id = $mail->getLastMessageID();
            echo "LastMessageID => " . $msg_id . "\n";

            //測(cè)收件
            $inbox = imap_open($imapServer, $imapUsername, $imapPassword);
            $result = imap_search($inbox, 'UNSEEN');

            foreach ($result as $mail_id) {
                $structure = imap_fetchstructure($inbox, $mail_id);
                for ($i = 0; $i < count($structure->parts); $i++) {
                    if ($structure->parts[$i]->ifdparameters) {
                        foreach ($structure->parts[$i]->dparameters as $object) {
                            if (strtolower($object->attribute) == 'filename') {
                                $filename = $object->value;
                                $file_data = imap_fetchbody($inbox, $mail_id, (string)($i + 1));
                                $file_mime = imap_fetchmime($inbox, $mail_id, (string)($i + 1));
                                $file_text = imap_fetchtext($inbox, $mail_id);
                                $file_structure = imap_fetchstructure($inbox, $mail_id);
                                // 處理附件內(nèi)容...
                                var_dump('讀取1 file:[' . $mail_id . ']', $filename, $file_mime, $file_text, $file_structure);

                                // 將附件保存到指定目錄
                                file_put_contents(self::attachmentPath . $filename, $file_data);
                            }
                        }
                    }
                }
            }
            imap_close($inbox);
        } catch (\Throwable $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            var_dump($e->getLine() . ' ' . $e->getMessage());
        }
        return response_api(200, 'success');
    }


    /** 方法二 郵件接收
     * @throws Exception
     */
    public function receiveMailWithAttachmentsImap()
    {
        $params = $this->request->post();
        $imapServer = $params['imapServer'] ?? self::imapServer; // QQ 郵箱的 IMAP 服務(wù)器地址
        $smtpServer = $params['smtpServer'] ?? self::imapServer; // QQ 郵箱的 smtp 服務(wù)器地址
        $imapUsername = $params['imapUsername'] ?? self::imapUsername; // 您的 QQ 郵箱地址
        $imapPassword = $params['imapPassword'] ?? self::imapPassword; // 您的 QQ 郵箱密碼
        $attachmentPath = $params['attachmentPath'] ?? self::attachmentPath; // 您的附件存放地址
        var_dump($imapServer, $smtpServer, $imapUsername, $imapPassword);
        $mailbox = new PHPMailer(true);
        $mailbox->isSMTP();
        $mailbox = new Mailbox($imapServer, $imapUsername, $imapPassword, $attachmentPath);

        $mailIds = $mailbox->searchMailbox('UNSEEN'); // 搜索未讀郵件
        var_dump('-------------2--------------', $mailIds);
        foreach ($mailIds as $mailId) {
            $email = $mailbox->getMail($mailId);

            // 獲取郵件主題、發(fā)件人、正文等信息
            $subject = $email->subject;//正文text內(nèi)容
            $from = $email->fromName;//發(fā)送者 **@qq.com
            $textHtml = $email->textHtml;//正文html內(nèi)容
            $date = $email->date;//收件時(shí)間
            $getAttachments = $email->getAttachments();//附件數(shù)組
            $fromAddress = $email->fromAddress;//來件者地址名稱 **@qq.com
            $fromName = $email->fromName;//來件者姓名

            var_dump($subject);
            var_dump($from);
            var_dump($textHtml);
            var_dump($date);
            var_dump($getAttachments);
            var_dump($fromAddress);
            var_dump($fromName);
            // 處理附件
//            foreach ($getAttachments as $attachment) {
//                $filename = $attachment->name;
//                $filePath = $attachment->filePath;
//
//                // 將附件保存到指定目錄
                file_put_contents($attachmentPath . $filename, $contents);
//            }

            // 在這里可以執(zhí)行你的邏輯,例如將郵件信息寫入數(shù)據(jù)庫或者進(jìn)行其他處理
        }
    }

}

方法二結(jié)果如圖所示:

以上就是php實(shí)現(xiàn)收發(fā)郵件的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于php收發(fā)郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • PHP寫API輸出的時(shí)用echo的原因詳解

    PHP寫API輸出的時(shí)用echo的原因詳解

    在本篇文章里小編給大家分享了關(guān)于PHP寫API輸出的時(shí)用echo的原因和相關(guān)知識(shí)點(diǎn),需要的朋友們參考下。
    2019-04-04
  • 一個(gè)PHP模板,主要想體現(xiàn)一下思路

    一個(gè)PHP模板,主要想體現(xiàn)一下思路

    一個(gè)PHP模板,主要想體現(xiàn)一下思路...
    2006-12-12
  • 詳解Laravel框架的依賴注入功能

    詳解Laravel框架的依賴注入功能

    依賴注入不是讓對(duì)象創(chuàng)建一個(gè)依賴關(guān)系,也不是讓工廠對(duì)象去創(chuàng)建對(duì)象,而是將所需的依賴變成一個(gè)外部對(duì)象,使之成為一個(gè)"某些人的問題”,你為"某些人的問題”注入了類的依賴關(guān)系。在Laravel中,這個(gè)"某人”是服務(wù)容器,服務(wù)容器負(fù)責(zé)通過構(gòu)造函數(shù)注入類的依賴關(guān)系。
    2021-05-05
  • php實(shí)現(xiàn)遞歸與無限分類的方法

    php實(shí)現(xiàn)遞歸與無限分類的方法

    這篇文章主要介紹了php實(shí)現(xiàn)遞歸與無限分類的方法,涉及php的遞歸操作技巧,需要的朋友可以參考下
    2015-02-02
  • php+mysql數(shù)據(jù)庫查詢實(shí)例

    php+mysql數(shù)據(jù)庫查詢實(shí)例

    這篇文章主要介紹了php+mysql數(shù)據(jù)庫查詢的方法,實(shí)例分析了數(shù)據(jù)庫查詢的原理與完整實(shí)現(xiàn)步驟,并進(jìn)行了針對(duì)性的分析說明,需要的朋友可以參考下
    2015-01-01
  • 一個(gè)圖片地址分解程序(用于PHP小偷程序)

    一個(gè)圖片地址分解程序(用于PHP小偷程序)

    這篇文章主要介紹了一個(gè)圖片地址分解程序,多用于用于PHP小偷程序,當(dāng)然也可以用于分析圖片地址的朋友這樣參考都可以獲取到了如果結(jié)合數(shù)據(jù)就更好了
    2014-08-08
  • 讓PHP以ROOT權(quán)限執(zhí)行系統(tǒng)命令的方法

    讓PHP以ROOT權(quán)限執(zhí)行系統(tǒng)命令的方法

    這種問題我想大家可能都遇到過,網(wǎng)友提供的解決方法也很多。我也只是結(jié)合自己系統(tǒng)的需求并結(jié)合網(wǎng)友的解決方案來總結(jié)的一種方法
    2011-02-02
  • php運(yùn)用memcache的完整實(shí)例

    php運(yùn)用memcache的完整實(shí)例

    這篇文章主要給大家介紹了關(guān)于php運(yùn)用memcache的完整實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • php 中英文語言轉(zhuǎn)換類代碼

    php 中英文語言轉(zhuǎn)換類代碼

    突然想做個(gè)中英文的功能試一下,只是把一些常用且有規(guī)律的詞匯比如 ‘評(píng)論’ ,時(shí)間單位(幾秒幾小時(shí)前這些)可以自由的轉(zhuǎn)化。
    2011-08-08
  • php 文章調(diào)用類代碼

    php 文章調(diào)用類代碼

    自己寫的MonolithCMS上面用到的文章類,可能不是很通用, 但是勝在我有全部的注釋
    2011-08-08

最新評(píng)論