Parser email yang teruji sepenuhnya untuk PHP 8.0+ (pembungkus ekstensi mailparse).
Ini adalah pengurai email PHP paling efektif dalam hal kinerja, pengkodean karakter asing, penanganan lampiran, dan kemudahan penggunaan. Format Pesan Internet RFC 822, 2822, 5322.
Ekstensi ini dapat digunakan untuk...
.eml
)Ya. Semua masalah yang diketahui telah direproduksi, diperbaiki, dan diuji.
Kami menggunakan GitHub Actions, Codecov, Codacy untuk membantu memastikan kualitas kode. Anda dapat melihat statistik real-time di bawah ini:
Cara termudah adalah melalui Komposer.
Untuk menginstal versi terbaru PHP MIME Mail Parser, jalankan perintah di bawah ini:
composer require php-mime-mail-parser/php-mime-mail-parser
Versi PHP berikut ini didukung:
Versi Sebelumnya:
Kompatibilitas PHP | Versi |
---|---|
HHVM | php-mime-mail-parser 2.11.1 |
PHP 5.4 | php-mime-mail-parser 2.11.1 |
PHP 5.5 | php-mime-mail-parser 2.11.1 |
PHP 5.6 | php-mime-mail-parser 3.0.4 |
PHP 7.0 | php-mime-mail-parser 3.0.4 |
PHP 7.1 | php-mime-mail-parser 5.0.5 |
PHP 7.2 | php-mime-mail-parser 7.1.2 |
PHP 7.3 | php-mime-mail-parser 7.1.2 |
PHP 7.4 | php-mime-mail-parser 7.1.2 |
Pastikan Anda telah menginstal ekstensi mailparse (http://php.net/manual/en/book.mailparse.php) dengan benar. Baris perintah php -m | grep mailparse
perlu mengembalikan "mailparse".
sudo apt install php-cli php-mailparse
brew install php
pecl install mailparse
sudo apt install php-cli php-pear php-dev php-mbstring
pecl install mailparse
AAAAMMDD seharusnya php-config --extension-dir
git clone https://github.com/php/pecl-mail-mailparse.git
cd pecl-mail-mailparse
phpize
./configure
sed -i 's/#ifs!HAVE_MBSTRING/#ifndef MBFL_MBFILTER_H/' ./mailparse.c
make
sudo mv modules/mailparse.so /usr/lib/php/AAAAMMDD/
echo "extension=mailparse.so" | sudo tee /etc/php/8.4/mods-available/mailparse.ini
sudo phpenmod mailparse
Anda perlu mengunduh DLL mailparse dari http://pecl.php.net/package/mailparse dan menambahkan baris extension=php_mailparse.dll
ke php.ini
.
Anda dapat memuat email dengan 4 cara berbeda:
require_once __DIR__ . ' /vendor/autoload.php ' ;
$ path = ' path/to/email.eml ' ;
$ parser = new PhpMimeMailParser Parser ();
// 1. Either specify a file path (string)
$ parser -> setPath ( $ path );
// 2. or specify the raw mime mail text (string)
$ parser -> setText ( file_get_contents ( $ path ));
// 3. or specify a php file resource (stream)
$ parser -> setStream ( fopen ( $ path , " r " ));
// 4. or specify a stream to work with a mail server (stream)
$ parser -> setStream ( fopen ( " php://stdin " , " r " ));
Dapatkan pengirim dan penerima:
$ rawHeaderTo = $ parser -> getHeader ( ' to ' );
// return "test" <[email protected]>, "test2" <[email protected]>
$ arrayHeaderTo = $ parser -> getAddresses ( ' to ' );
// return [["display"=>"test", "address"=>"[email protected]", false]]
$ rawHeaderFrom = $ parser -> getHeader ( ' from ' );
// return "test" <[email protected]>
$ arrayHeaderFrom = $ parser -> getAddresses ( ' from ' );
// return [["display"=>"test", "address"=>"[email protected]", "is_group"=>false]]
Dapatkan subjeknya:
$ subject = $ parser -> getHeader ( ' subject ' );
Dapatkan header lainnya:
$ stringHeaders = $ parser -> getHeadersRaw ();
// return all headers as a string, no charset conversion
$ arrayHeaders = $ parser -> getHeaders ();
// return all headers as an array, with charset conversion
$ text = $ parser -> getMessageBody ( ' text ' );
// return the text version
$ html = $ parser -> getMessageBody ( ' html ' );
// return the html version
$ htmlEmbedded = $ parser -> getMessageBody ( ' htmlEmbedded ' );
// return the html version with the embedded contents like images
Simpan semua lampiran dalam direktori
$ parser -> saveAttachments ( ' /path/to/save/attachments/ ' );
// return all attachments saved in the directory (include inline attachments)
$ parser -> saveAttachments ( ' /path/to/save/attachments/ ' , false );
// return all attachments saved in the directory (exclude inline attachments)
// Save all attachments with the strategy ATTACHMENT_DUPLICATE_SUFFIX (default)
$ parser -> saveAttachments ( ' /path/to/save/attachments/ ' , false , Parser:: ATTACHMENT_DUPLICATE_SUFFIX );
// return all attachments saved in the directory: logo.jpg, logo_1.jpg, ..., logo_100.jpg, YY34UFHBJ.jpg
// Save all attachments with the strategy ATTACHMENT_RANDOM_FILENAME
$ parser -> saveAttachments ( ' /path/to/save/attachments/ ' , false , Parser:: ATTACHMENT_RANDOM_FILENAME );
// return all attachments saved in the directory: YY34UFHBJ.jpg and F98DBZ9FZF.jpg
// Save all attachments with the strategy ATTACHMENT_DUPLICATE_THROW
$ parser -> saveAttachments ( ' /path/to/save/attachments/ ' , false , Parser:: ATTACHMENT_DUPLICATE_THROW );
// return an exception when there is attachments duplicate.
Dapatkan semua lampiran
$ attachments = $ parser -> getAttachments ();
// return an array of all attachments (include inline attachments)
$ attachments = $ parser -> getAttachments ( false );
// return an array of all attachments (exclude inline attachments)
Ulangi semua lampiran
foreach ( $ attachments as $ attachment ) {
echo ' Filename : ' . $ attachment -> getFilename (). ' <br> ' ;
// return logo.jpg
echo ' Filesize : ' . filesize ( $ attach_dir . $ attachment -> getFilename ()). ' <br> ' ;
// return 1000
echo ' Filetype : ' . $ attachment -> getContentType (). ' <br> ' ;
// return image/jpeg
echo ' MIME part string : ' . $ attachment -> getMimePartStr (). ' <br> ' ;
// return the whole MIME part of the attachment
$ stream = $ attachment -> getStream ();
// get the stream of the attachment file
$ attachment -> save ( ' /path/to/save/myattachment/ ' , Parser:: ATTACHMENT_DUPLICATE_SUFFIX );
// return the path and the filename saved (same strategy available than saveAttachments)
}
Untuk meneruskan email dari Postfix ke skrip PHP di atas, tambahkan baris ini di akhir /etc/postfix/master.cf
Anda (untuk menentukan myhook untuk mengirim semua email ke skrip test.php
):
myhook unix - n n - - pipe
flags=F user=www-data argv=php -c /etc/php5/apache2/php.ini -f /var/www/test.php ${sender} ${size} ${recipient}
Edit baris ini (daftarkan myhook)
smtp inet n - - - - smtpd
-o content_filter=myhook:dummy
Skrip PHP harus menggunakan metode keempat (lihat di atas) agar dapat bekerja dengan konfigurasi ini.
Dan terakhir cara termudah adalah menggunakan SaaS saya https://mailcare.io
Jangan ragu untuk berkontribusi!
git clone https://github.com/php-mime-mail-parser/php-mime-mail-parser
cd php-mime-mail-parser
composer install
./vendor/bin/phpunit
Jika Anda melaporkan masalah, harap berikan email mentah yang memicu masalah tersebut. Hal ini membantu kami mereproduksi masalah dan memperbaikinya dengan lebih cepat.
php-mime-mail-parser/php-mime-mail-parser adalah perangkat lunak sumber terbuka yang dilisensikan di bawah lisensi MIT