*我在发送邮件时遇到问题,此脚本在 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);
}
?>