PHP 8.0+(mailparse 확장 래퍼)용으로 완전히 테스트된 이메일 파서입니다.
성능, 외국 문자 인코딩, 첨부 파일 처리 및 사용 편의성 측면에서 가장 효과적인 PHP 이메일 파서입니다. 인터넷 메시지 형식 RFC 822, 2822, 5322.
이 확장 기능은 다음과 같은 용도로 사용할 수 있습니다.
.eml
)예. 알려진 모든 문제가 재현, 수정 및 테스트되었습니다.
우리는 GitHub Actions, Codecov, Codacy를 사용하여 코드 품질을 보장합니다. 아래에서 실시간 통계를 볼 수 있습니다.
가장 쉬운 방법은 Composer를 이용하는 것입니다.
PHP MIME 메일 파서의 최신 버전을 설치하려면 아래 명령을 실행하십시오.
composer require php-mime-mail-parser/php-mime-mail-parser
다음 버전의 PHP가 지원됩니다.
이전 버전:
PHP 호환성 | 버전 |
---|---|
HHVM | PHP-MIME-메일-파서 2.11.1 |
PHP 5.4 | PHP-MIME-메일-파서 2.11.1 |
PHP 5.5 | PHP-MIME-메일-파서 2.11.1 |
PHP 5.6 | PHP-MIME-메일-파서 3.0.4 |
PHP 7.0 | PHP-MIME-메일-파서 3.0.4 |
PHP 7.1 | PHP-MIME-메일-파서 5.0.5 |
PHP 7.2 | PHP-MIME-메일-파서 7.1.2 |
PHP 7.3 | PHP-MIME-메일-파서 7.1.2 |
PHP 7.4 | PHP-MIME-메일-파서 7.1.2 |
mailparse 확장(http://php.net/manual/en/book.mailparse.php)이 제대로 설치되어 있는지 확인하세요. 명령줄 php -m | grep mailparse
"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는 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
http://pecl.php.net/package/mailparse에서 mailparse DLL을 다운로드하고 이에 따라 extension=php_mailparse.dll
줄을 php.ini
에 추가해야 합니다.
다음 4가지 방법으로 이메일을 로드할 수 있습니다.
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 " ));
발신자와 수신자를 가져옵니다.
$ 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]]
주제를 얻으세요:
$ subject = $ parser -> getHeader ( ' subject ' );
다른 헤더 가져오기:
$ 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
모든 첨부 파일을 디렉토리에 저장
$ 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.
모든 첨부파일 가져오기
$ 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)
모든 첨부 파일을 반복합니다.
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)
}
Postfix에서 위의 PHP 스크립트로 메일을 전달하려면 /etc/postfix/master.cf
끝에 다음 줄을 추가하세요(모든 이메일을 test.php
스크립트로 보내도록 myhook을 지정하려면):
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}
이 줄을 편집하세요(myhook 등록).
smtp inet n - - - - smtpd
-o content_filter=myhook:dummy
이 구성을 사용하려면 PHP 스크립트에서 네 번째 방법(위 참조)을 사용해야 합니다.
마지막으로 가장 쉬운 방법은 SaaS https://mailcare.io를 사용하는 것입니다.
자유롭게 기여해 주세요!
git clone https://github.com/php-mime-mail-parser/php-mime-mail-parser
cd php-mime-mail-parser
composer install
./vendor/bin/phpunit
문제를 보고하는 경우 문제를 발생시킨 원본 이메일을 제공해 주세요. 이렇게 하면 문제를 재현하고 더 빨리 해결하는 데 도움이 됩니다.
php-mime-mail-parser/php-mime-mail-parser는 MIT 라이센스에 따라 라이센스가 부여된 오픈 소스 소프트웨어입니다.