簡體   English   中英

從PHP發送電子郵件不起作用

[英]Sending email from PHP is not working

首先,我將對PHP完全一無所知。 我是.NET。

我將這個電子郵件模板從Internet上撤了下來-並按照說明進行了操作,只是將“收件人”電子郵件地址和“主題”行更改為我想要的任何內容。 在我的PHP文件的最后,我有“謝謝您與我們聯系。我們將盡快與您聯系。” -當我單擊提交時,此消息將顯示在我的瀏覽器中。 它思考了一會兒,然后顯示消息,向我指示已發送電子郵件。 但是我沒有收到任何電子郵件。 有什么想法嗎?

這是我的HTML表單代碼:

<form name="contactform" method="post" action="MailHandler.php">
    <table width="450px">
        <tr>
            <td valign="top">
                <label for="first_name">First Name *</label>
            </td>
            <td valign="top">
                <input type="text" name="first_name" maxlength="50" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="last_name">
                Last Name *</label>
            </td>
            <td valign="top">
                <input type="text" name="last_name" maxlength="50" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="email">Email Address *</label>
            </td>
            <td valign="top">
                <input type="text" name="email" maxlength="80" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="telephone">Telephone Number</label>
            </td>
            <td valign="top">
                <input type="text" name="telephone" maxlength="30" size="30">
            </td>
        </tr>
        <tr>
            <td valign="top">
                <label for="comments">Comments *</label>
            </td>
            <td valign="top">
                <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center">
                <br/>
                <input type="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

這是MailHandler.php:

<?php

    if(isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "mike@mikemarks.net";
        $email_subject = "Your email subject line";

        function died($error) {

            // Your error code can go here

            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        // Validation expected data exists

        if(!isset($_POST['first_name']) ||
            !isset($_POST['last_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }

        $first_name = $_POST['first_name']; // Required
        $last_name  = $_POST['last_name']; // Required
        $email_from = $_POST['email']; // Required
        $telephone  = $_POST['telephone']; // Not required
        $comments   = $_POST['comments']; // required

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

        if(!preg_match($email_exp,$email_from)) {
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";

        if(!preg_match($string_exp,$first_name)) {

          $error_message .= 'The First Name you entered does not appear to be valid.<br />';
        }

        if(!preg_match($string_exp,$last_name)) {

          $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
        }

        if(strlen($comments) < 2) {

          $error_message .= 'The Comments you entered do not appear to be valid.<br />';
        }

        if(strlen($error_message) > 0) {

          died($error_message);
        }

        $email_message = "Form details below.\n\n";

        function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "First Name: ".clean_string($first_name)."\n";
        $email_message .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";

        // Create email headers

        $headers = 'From: '.$email_from."\r\n".
                   'Reply-To: '.$email_from."\r\n" .
                   'X-Mailer: PHP/' . phpversion();
                   @mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- Include your own success HTML here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

    }
?>

該代碼看起來合法。 詢問您的托管公司是否支持PHP驅動的電子郵件...

我自己在PHP中使用過電子郵件。 這些變量會自動從我的HTML文件傳遞到您所稱的mailHandler.php文件。 他是我如何使用郵件系統的示例。 希望對您有所幫助。

<?php
$to = $_POST['emailaddress'];
$subject = 'subject here';
$message = '<html><head><style type="text/css">Insert any css here</style></head><body>';
$message .= 'Any HTML like you were designing a standard screen display';
$from = 'emailaddress';
$headers = 'MIME-Version: 1.0' . "\r\n';
$headers .= "Content-type: text/html; charset=iso=8859-1' . "\r\n";
$headers .= "From: $email_from";
mail ($to,$subject,$message,$headers);

echo "<input type='button' value=\"Click to Close \" onclick=\"window.open('urlHere.php','_self');\" />";
?>

這是一個簡單的版本,但是它允許您創建/發送模擬電子郵件本身內的網站的電子郵件。 只需在附加的$ message。=''內逐行添加任何HTML代碼。 部分。

我希望這可以幫到你。 這是我做電子郵件的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM