服务器和邮件功能

时间:2013-03-14 23:03:20

标签: php email server-side

* 我发送邮件时遇到问题 此脚本在localhost上有效工作,但不在服务器上工作 请帮助请问localhost和其他服务器上的邮件功能之间是否有区别*

<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);

/**
* this function is made to upload multiple files
*@param string $file (input name)
*@return nothing
*/
function upload($file)
{
    for($i=0; $i<count($_FILES[$file]['name']); $i++) {
  //Get the temp file path
  $file_name = $_FILES[$file]['name'][$i];
  $type = $_FILES[$file]['type'][$i];
  $tmpFilePath = $_FILES[$file]['tmp_name'][$i];
  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
   global $newFilePath;
   $newFilePath  = "uploadFiles/" . $_FILES[$file]['name'][$i];
   global $files;
   $files[] = array($newFilePath.':'.$type.':'.$file_name);
   //echo $files;


   //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

     echo "تم رفع الملفات بنجاح";

    }
    else{
        echo "حدث خطا فى رفع الملفات";
    }
  }
}
}






if($_POST['contact'])
{



    if($_POST['name'] && $_POST['email'] && $_POST['phone'] && $_POST['others'] && $_FILES['img'])
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $others = $_POST['others'];
        $others = implode(" and ", $others);
        upload("img");
      foreach ($files as $index => $file) {
  foreach ($file as   $value) {
    $a[] = $value;
  }
}
}
    else
    {
      $_SESSION['message'] = "please fill all require fields";
      header('Location: contact.php');
    }
}



?>

<?php
$RecipientEmail = "mohammed_habibphp@yahoo.com"; 

$RecipientName = "mohammed habib";

$SenderEmail = $email; 

$SenderName = $name;

$subject = "coding";

$cc = "alice@example.com, bob@example.com"; 

$bcc = "johndoe@example.com";


// For HTML
$type = "HTML";
// For plain text
//$type = "plain";


$message ="<b>customer name</b>: ".$name.


"<br /><b>email</b>: ".$email.


"<br /><b>phone</b>: ".$phone.


"<br /><b>others</b>: ".$others;





// For normal leave it blank
$priority = "high";

$attachments =$a;


$sent = Email($RecipientEmail, 
              $RecipientName, 
              $SenderEmail, 
              $SenderName, 
              $cc, 
              $bcc, 
              $subject, 
              $message, 
              $attachments, 
              $priority, 
              $type);

echo $sent ? "It worked!!" : "It didn't work"; 
// ^ This is used to test if it worked

// This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License

function Email($remail, $rname, $semail, $sname, $cc, $bcc, $subject, $message, $attachments, $priority, $type)  {

// Checks if carbon copy & blind carbon copy exist
if($cc != null){$cc="CC: ".$cc."\r\n";}else{$cc="";}
if($bcc != null){$bcc="BCC: ".$bcc."\r\n";}else{$bcc="";}

// Checks the importance of the email
if($priority == "high"){$priority = "X-Priority: 1\r\nX-MSMail-Priority: High\r\nImportance: High\r\n";}
elseif($priority == "low"){$priority = "X-Priority: 3\r\nX-MSMail-Priority: Low\r\nImportance: Low\r\n";}
else{$priority = "";}

// Checks if it is plain text or HTML
if($type == "plain"){$type="text/plain";}else{$type="text/html";}

// The boundary is set up to separate the segments of the MIME email
$boundary = md5(@date("Y-m-d-g:ia"));

// The header includes most of the message details, such as from, cc, bcc, priority etc. 
$header = "From: ".$sname." <".$semail.">\r\nMIME-Version: 1.0\r\nX-Mailer: PHP\r\nReply-To: ".$sname." <".$semail.">\r\nReturn-Path: ".$sname." <".$semail.">\r\n".$cc.$bcc.$priority."Content-Type: multipart/mixed; boundary = ".$boundary."\r\n\r\n";    

// The full message takes the message and turns it into base 64, this basically makes it readable at the recipients end
$fullmessage = "--".$boundary."\r\nContent-Type: ".$type."; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n".chunk_split(base64_encode($message));

// A loop is set up for the attachments to be included.
if($attachments != null) {
  foreach ($attachments as $attachment)  {
    $attachment = explode(":", $attachment);
    print_r($attachment);

    $fullmessage .= "--".$boundary."\r\nContent-Type: ".$attachment[1]."; name=\"".$attachment[2]."\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n\r\n".chunk_split(base64_encode(file_get_contents($attachment[0])));
  }
}

// And finally the end boundary to set the end of the message
$fullmessage .= "--".$boundary."--";

return mail($rname."<".$remail.">", $subject, $fullmessage, $header);
}

?>  

1 个答案:

答案 0 :(得分:0)

PHP 4.2.3+中,第4和第5个参数在 safe_mode 中被禁用,mail()函数将显示警告消息,并在使用时返回FALSE。

换句话说,您的服务器可能已启用 safe_mode 。并使用第4个参数:

return mail($rname."<".$remail.">", $subject, $fullmessage, $header);

也可能是服务器的sendmail设置不正确或根本没有安装的可能性很小。检查phpinfo()以查看是否已启用/注册。如果所有其他方法都失败,请调用您的Web服务器主机或提交票证。