smtpプロトコルphpでemail関数を送信(メール)
ネット上でemailを送る関数を探して、ほとんど探し当てていないで、メールを送る類があって、自分で1つのsmtpのを書いてみましたが、メールを送るのはまあまあですが、まだどのように添付ファイルを送るか分かりません.まず記録して、それから研究して、ゆっくり更新します.
163のsmtpをテストし、送信できます.
163のsmtpをテストし、送信できます.
function sendEmail($host, $port=25, $check=0, $username='', $password='', $fromemail, $toemail, $fromName=''
, $toName='', $subject='', $message='', $additional='', $charset='utf-8')
{
$CRLF = "\r
";
$fromName = trim($fromName) == '' ? $fromemail : $fromName;
$toName = trim($toName) == '' ? $toemail : $toName;
$send_from = "=?$charset?B?".base64_encode($fromName)."?= <$fromemail>";
$send_to = "=?$charset?B?".base64_encode($toName)."?= <$toemail>";
$send_subject = "=?$charset?B?".base64_encode(str_replace(array("\r","
"), array('',' '),$subject)).'?=';
$send_message = chunk_split(base64_encode(str_replace("\r
.", " \r
..", str_replace("
", "\r
",
str_replace("\r", "
", str_replace("\r
", "
", str_replace("
\r", "\r", $message)))))));
$additional = "To: {$send_to}{$CRLF}From: {$send_from}{$CRLF}MIME-Version: 1.0{$CRLF}Content-type: text/html; charset=$charset{$CRLF}{$additional}Content-Transfer-Encoding: base64{$CRLF}";
$fp = fsockopen($host, $port, $errno, $errstr);
if (!$fp)
{
return 'SCE'; //smtp connect error
}
if (strncmp(fgets($fp, 512), '220', 3) != 0)
{
return 'SCE'; //smtp connect error
}
fwrite($fp, "EHLO $fromName{$CRLF}");
if ($check)
{
while ($rt=strtolower(fgets($fp, 512)))
{
if (strpos($rt, "-") !== 3 || empty($rt))
{
break;
}
elseif (strpos($rt, "2") !== 0)
{
return "AE";//auth error
}
}
fwrite($fp, "AUTH LOGIN{$CRLF}");
if (strncmp(fgets($fp, 512), '334', 3) != 0)
{
return "ALE";//auth login error
}
fwrite($fp, base64_encode($username).$CRLF);
if (strncmp(fgets($fp,512), '334', 3) != 0)
{
return "ALUE";//auth login username error
}
fwrite($fp, base64_encode($password).$CRLF);
if (strncmp(fgets($fp, 512), '235', 3) != 0)
{
return 'ALPE';//auth login password error
}
}
$from = preg_replace("/.*\<(.+?)\>.*/", "\\1", $fromemail);
fwrite($fp, "MAIL FROM: <$from>$CRLF");
if (strncmp(fgets($fp, 512), '250', 3) != 0)
{
return 'EFE'; //email from error
}
fwrite($fp, "RCPT TO: <$toemail>$CRLF");
if (strncmp(fgets($fp,512), '250', 3) != 0)
{
return 'ETE';//email toemail error
}
fwrite($fp, "DATA$CRLF");
if (strncmp(fgets($fp, 512), '354', 3) != 0)
{
return 'EDE';//email data error
}
$msg = "Date: ".Date("r").$CRLF;
$msg .= "Subject: $send_subject".$CRLF;
$msg .= $additional.$CRLF;
$msg .= $send_message.$CRLF.".".$CRLF;
fwrite($fp, $msg);
$lastmessage = fgets($fp, 512);
if (substr($lastmessage, 0, 3) != 250)
{
return 'ESE';//email send error
}
fwrite($fp, "QUIT\r
");
fclose($fp);
return 'OK';
}