使用PHPMailer的PHP电子邮件不起作用


php email using PHPMailer not working

我在通过我的网络表单发送电子邮件时遇到问题,我正在使用PHP和PHPMailer类

我的表单代码 iS

<form class="cd-form floating-labels" method="post" action="email.php" enctype="multipart/form-data">
    <fieldset>
        <legend>Contact Us</legend>
        <div class="icon">
            <label class="cd-label" for="cd-name">Name</label>
            <input class="user" type="text" name="cd-name" id="cd-name" value="<?php if(isset($_POST["cd-name"])){echo $_POST["cd-name"];}else{echo "";}?> required>
        </div> 
        <div class="icon">
            <label class="cd-label" for="cd-company">Company</label>
            <input class="company" type="text" name="cd-company" id="cd-company" required>
        </div> 
        <div class="icon">
            <label class="cd-label" for="cd-email">Email</label>
            <input class="email" type="email" name="cd-email" id="cd-email" required>
        </div>
        <div class="icon">
            <label class="cd-label" for="cd-phone">Contact No</label>
            <input class="phone" type="text" name="cd-phone" id="cd-phone" required>
        </div>

    </fieldset>
    <fieldset>
        <div class="icon">
            <label class="cd-label" for="cd-message">Project description</label>
            <textarea class="message" name="cd-message" id="cd-message" required></textarea>
        </div>
    <div class='icon'>
        <label class="cd-upload" for='cd-upload' >Another upload:</label>
        <input type="file" name='cd-upload' id='cd-upload' />
    </div>
    <div>
        <p class="cd-select icon">
            <select name="cd-hearsay" class="budget">
                <option value="0">Where Did You Hear About Us?</option>
                <option value="1">Google</option>
                <option value="2">Friend / Family</option>
                <option value="3">Ad On A Website</option>
            </select>
        </p>
    </div>
    <div class="icon">
            <label class="cd-label" for="cd-coupon">Coupon Code</label>
            <input class="coupon" type="text" name="cd-coupon" id="cd-name">
        </div>             
        <div>
            <input type="submit" value="Send Message">
        </div>
    </fieldset>
</form>

和我的电子邮件.php代码是

    <?php
require("class.phpmailer.php");
// define variables and set to empty values
$name = $company = $email = $phone = $message = $upload = $hearsay = $coupon =  "";
$nameErr = $companyErr = $emailErr = $phoneErr = $messageErr = $uploadErr = $hearsayErr = $couponErr =  "";

function clean($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
//clean varibles
$company = clean($_POST['cd-company']);
$upload = clean($_POST['cd-upload']);
$hearsay = clean($_POST['cd-hearsay']);
$coupon = clean($_POST['cd-coupon']);
//validate varibles
//Check Name
if(empty($_POST['cd-name']))
{
    $nameErr = "Name Field is Required";
}
elseif(strlen($_POST['cd-name']) <= 3)
{
    $nameErr = "Please provide your full name";
}
elseif(!ctype_alpha($_POST['cd-name']))
{
    $nameErr = "Please only use letters in your name";
}
else
{
    $name = clean($_POST['cd-name']);
}
//Check Email
if (empty($_POST["cd-email"])) 
{
    $emailErr = "Email is required";
}
elseif(!filter_var($_POST["cd-email"], FILTER_VALIDATE_EMAIL))
{
    $emailErr = "Invalid Email Format"; 
} 
else
{
    $email = clean($_POST["cd-email"]);
}
//Check Phone
if(empty($_POST['cd-phone']))
{
    $phoneErr = "Phone number is required";
}
elseif(!is_numeric($_POST['cd-phone']))
{
    $phoneErr = "Please use numers only";
}
else
{
    $phone = clean($_POST['cd-phone']);
}
//Check Message
if(empty($_POST['cd-message']))
{
    $messageErr = "Message is blank!";
}
elseif(!ctype_alnum($_POST['cd-message']))
{
    $messageErr = "Please use numbers and letters only";    
}
else
{
    $message = clean($_POST['cd-message']);
}
//Check Upload File Types / Size
if(!empty($_POST['cd-upload']))
{
    //Get the uploaded file information
    $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
    //get the file extension of the file
    $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
    $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs
    //Settings
    $max_allowed_file_size = 100; // size in KB
    $allowed_extensions = array("doc", "xls", "txt");
    //Validations
    if($size_of_uploaded_file > $max_allowed_file_size )
    {
      $uploadErr .= "'n Size of file should be less than $max_allowed_file_size";
    }
    //------ Validate the file extension -----
    $allowed_ext = false;
    for($i=0; $i<sizeof($allowed_extensions); $i++)
    {
      if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
      {
        $allowed_ext = true;
      }
    }
    if(!$allowed_ext)
    {
      $uploadErr .= "'n The uploaded file is not supported file type. ".
      " Only the following file types are supported: ".implode(',',$allowed_extensions);
    }
    //copy the temp. uploaded file to uploads folder
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
    if(is_uploaded_file($tmp_path))
    {
      if(!copy($tmp_path,$path_of_uploaded_file))
      {
        $uploadErr .= ''n error while copying the uploaded file';
      }
    }
}

if(!$nameErr && !$emailErr && !$phoneErr && !$messageErr && !$uploadErr)
{
    echo "no errors";
    $mail = new PHPMailer();
    $mail->IsSMTP();                                      // set mailer to use SMTP
    $mail->Host = "send.one.com";  // specify main and backup server
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->Username = "jswan";  // SMTP username
    $mail->Password = "secret"; // SMTP password
    $mail->From = $email;
    $mail->FromName = $name;
    $mail->AddAddress("testemail@hotmail.co.uk", "Looty Jackinson");
    #$mail->AddAddress("ellen@example.com");                  // name is optional
    $mail->AddReplyTo("info@example.com", "Information");
    $mail->WordWrap = 50;                                 // set word wrap to 50 characters
    $mail->AddAttachment("uploads");         // add attachments
    #$mail->AddAttachment("uploads/image.jpg", "new.jpg");    // optional name
    $mail->IsHTML(true);                                  // set email format to HTML
    $mail->Subject = "Here is the subject";
    $mail->Body    = "This is the HTML message body <b>in bold!</b>";
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    if(!$mail->Send())
    {
       echo "Message could not be sent. <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
       exit;
    }
    echo "Message has been sent";
}
else
{
    echo "errors";
    print_r($nameErr);
    print_r($emailErr);
    print_r($phoneErr);
    print_r($uploadErr);    
    #header("index.php");
}
?>

但这会导致以下错误,没有错误无法访问文件:上传警告:require_once(class.smtp.php):无法打开流:在/customers/2/9/2/kernow-connect.com/httpd.www/contact-form/class.phpmailer.php 第 760 行中没有此类文件或目录 致命错误:require_once():打开失败需要"class.smtp.php"(include_path='.:/usr/share/php') 在/customers/2/9/2/kernow-connect.com/httpd.www/contact-form/class.phpmailer.php 第 760 行

我真的不确定我现在在做什么,非常感谢一些帮助!

您可以在 http://www.kernow-connect.com/contact-form/form.php 查看问题

如果需要.php我可以发布 PHPMailer 的内容非常感谢

卢克

您没有阅读文档(自述文件告诉您需要了解的内容)或 PHPMailer 提供的示例代码,并且您可能使用的是旧版本。引用自述文件:

至少你需要class.phpmailer.php。如果您使用的是 SMTP,则需要 class.smtp.php,如果您使用的是 POP-before SMTP,则需要 class.pop3.php。对于所有这些,我们建议您也使用自动加载器,否则您将不得不手动要求所有类或使用其他自动加载器

发布前先查看。