Site icon 时鹏亮的Blog

PHP-利用fsockopen函数SMTP发送邮件-支持附件

请知悉:本文最近一次更新为 10年 前,文中内容可能已经过时。
<?php
function SendMail($ServerName, $Port, $Username, $Password, $ToEmail, $FromEmail, $Subject, $Body, $Attachments = '')
{
/* Attachments work like this array('Attachment Name'=>'ATTACHMENT AS A STRING','Next Attachment Name'=>'ATTACHMENT 2 AS A STRING') */

$smtp = fsockopen($ServerName, $Port);
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 220;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed Connect";

fputs($smtp, "HELO $ServerNamen");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 250;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed Helo";

fputs($smtp, "AUTH LOGINn");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 334;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed AUTH";

fputs($smtp, base64_encode($Username) . "n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 334;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed Username";

fputs($smtp, base64_encode($Password) . "n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 235;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed Password";

fputs($smtp, "MAIL From:<$FromEmail>n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 250;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed MAIL";

fputs($smtp, "RCPT To:<$ToEmail>n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 250;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed RCPT";

fputs($smtp, "DATAn");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 354;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed DATA";

fputs($smtp, "From: $FromEmailrn");
fputs($smtp, "To: $ToEmailrn");
if ($Attachments != '') {
$ContentBoundry = '----=_NextPart_' . md5(date('U') . uniqid('NCF', TRUE));
fputs($smtp, "MIME-Version: 1.0rn");
fputs($smtp, 'Content-Type: multipart/mixed; boundary="' . $ContentBoundry . '"' . "rn");
}
fputs($smtp, "Subject: $Subjectrnrn");

if ($Attachments == '') {
fputs($smtp, "$Bodyrn.rn");
} else {
$NewBody = "rn";
$NewBody .= '--' . $ContentBoundry . "rn";
$NewBody .= 'Content-Type: text/plain; charset="UTF-8"' . "rn";
$NewBody .= 'Content-Transfer-Encoding: 8bit' . "rnrn";
$NewBody .= $Body . "rn";
foreach ($Attachments as $Filename => $Attachment) {
$NewBody .= '--' . $ContentBoundry . "rn";
$NewBody .= 'Content-Type: application/octet-stream; name="' . $Filename . '"' . "rn";
$NewBody .= 'Content-Transfer-Encoding: base64' . "rn";
$NewBody .= 'Content-Disposition: attachment; filename="' . $Filename . '"' . "rnrn";
$NewBody .= chunk_split(base64_encode($Attachment));
$NewBody .= "rn";
}
$NewBody .= $ContentBoundry . "--rnrn";
fputs($smtp, $NewBody . "rn.rn");
}

$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 250;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed BODY";

fputs($smtp, "QUITn");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 221;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed QUIT";

fclose($smtp);

return true;
}

上面是函数的源码,使用方法:

$TheAttachments = array(
'CurriculumReviewCycle.pdf' => file_get_contents('/var/www/html/CurriculumReviewCycle.pdf')
);
// SendMail($ServerName, $Port, $Username, $Password, $ToEmail, $FromEmail, $Subject, $Body, $Attachments='' )
SendMail('smtp.gmail.com', 25, 'franklinc@fromdomain', 'Password', 'cfranklin@todomain', 'franklinc@fromdomain', 'Attach - Test - subject', 'What up dog! - the body', $TheAttachments);

全文代码完整来自:http://www.nomadcf.com/scripting/php-sendemailsockets-withattachments


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《PHP-利用fsockopen函数SMTP发送邮件-支持附件》相关的博文:

Exit mobile version