<?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 $ServerName\n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 250;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed Helo";

fputs($smtp, "AUTH LOGIN\n");
$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, "DATA\n");
$InputBuffer = fgets($smtp, 1024);
$ErrorCode = 354;
if (substr($InputBuffer, 0, 3) != "$ErrorCode")
return "Failed DATA";

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

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

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

fputs($smtp, "QUIT\n");
$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, [email protected]', 'Password', [email protected]', [email protected]', 'Attach - Test - subject', 'What up dog! - the body', $TheAttachments);

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

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