یکی از صفحات پرکاربرد هر سایت و مورد علاقه Webmaster ها ، صفحه "تماس با ما" می باشد که جهت ارتباط بازدید کنندگان سایت با واحدهای مختلف آن سازمان، شرکت یا شخص استفاده می شود. جهت افزایش امنیت در ارسال ایمیل و جلوگیری از ارسال SPAM توسط BOT ها، می بایست ارسال ایمیل در form مربوطه، با Authenticate انجام گیرد. بدین صورت که جهت ارسال ایمیل از طریق Form برنامه، می بایست حساب ایمیل ایجاد شده در کنترل پنل میزبانی دامنه، به همراه کلمه عبور و آدرس SMTP سرور، در
کد برنامه ذکر گردد.
نمونه کد مربوط به ارسال ایمیل با Authenticate روی سرورهای پارس دیتا در پلنهای ویندوزی، به شرح ذیل می باشد.
<?
$to = "test@yahoo.com";
$from = "support@parsdata.com";
$password = "your email password";
$subject = "this is subject";
$contents = "This is a test";
PARSDATA_Send_Mail($from,$to,$password,$subject,$contents);
print("<HTML><BODY>Mail sent</body></html>!");
function PARSDATA_Send_Mail($from, $to,$password, $subject, $message)
{
$namefrom = "";
$nameto = "";
/* your configuration here */
$smtpServer = "Mail Server IP"; //does not accept STARTTLS
$port = "25"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = $from; //your gmail account
$password = $password; //the pass for your gmail
$localhost = $_SERVER['REMOTE_ADDR']; //requires a real ip
$newLine = "\r\n"; //var just for newlines
/* you shouldn't need to mod anything else */
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
//echo $errstr." - ".$errno;
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else
{
//$logArray['connection'] = "Connected to: $smtpResponse";
// echo "connection accepted<br>".$smtpResponse."<p />Continuing<p
//>";
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}
?>
نمونه کد مربوط به ارسال ایمیل
با Authenticate روی سرورهای پارس دیتا در پلنهای لینوکسی، به شرح
ذیل می باشد.
<?php
require_once "Mail.php";
$from = "support@parsdata.com";
$to = "test@yahoo.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "localhost";
$username = "support@parsdata.com";
$password = "your email password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
نمونه کد مربوط به ارسال ایمیل
بدون Authenticate روی سرورهای پارس دیتا به شرح
ذیل می باشد.
<?
$recipient = "you@gmail.com";
$subject = "hello";
$forminfo =
($_POST['Name'] . "\r" .
$_POST['Email'] . "\r" .
$_POST['Subject'] . "\r\n" .
$_POST['Message'] . "\r\n" .
date("Y-M-d") . "\r\n\n");
$formsend = mail("$recipient", "$subject", "$forminfo", "From: $Email\r\nReply-to:$Email");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
</body>
</html>
ایمیلی که در این کد قرار می گیرد، می بایست ایميلی باشد که در کنترل پنل میزبانی دامنه مورد نظر، تعریف شده باشد. برای مثال در صورتی که قصد ارسال ایمیل از طریق form سایت parsdata.info را دارید، ایمیلی که دراین کد قرار می دهید، می بایست مربوط به همین دامنه (فرضا info@parsdata.info) باشد. قرار دادن ایمیلهای متفرقه از جمله yahoo، Gmail، Hotmail و... یا ایمیلهای تعریف شده در سرویس های میزبانی دیگر، به غیر از ایمیل تعریف شده در کنترل میزبانی دامنه مورد نظر، در این کد، اشتباه است و کار نخواهد کرد.
در صورتی که پلن میزبانی ویندوزی می باشد، آدرس IP سرور Mail را می توانید با استفاده از دستور ذیل در Command Prompt، بدست آورید.
ping mail.yourdomain.com
در صورتی که پلن میزبانی ویندوزی می باشد، می توان به جای IP عبارت mail.yourdomain.com را قرار داد.