<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com';
    $to = 'contact@yoursite.com';
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent!</p>';
        } else {
            echo '<p>Something went wrong, go back and try again!</p>';
        }
    }
?>

我试着创建了一个简单的邮件表单。表单本身在我的index.html页面上,但它提交到一个单独的“感谢您的提交”页面thanks . PHP,其中嵌入了上述PHP代码。 代码完美地提交,但从未发送电子邮件。我该如何解决这个问题?


当前回答

如果你受困于Hostgator上的应用,这便是解决我的问题的方法。非常感谢发布详细解决方案的人。如果有一天链接离线,你有总结:

在服务器中查找sendmail路径。一个简单的方法来检查它,是临时写以下代码在一个页面,只有你将访问,以读取生成的信息:php phpinfo ();? >。打开这个页面,寻找sendmail路径。(然后,别忘了删除这段代码!) 问题和修正:如果你的sendmail路径只说-t -i,那么编辑你的服务器的php.ini并添加以下行:sendmail_path = /usr/sbin/sendmail -t -i;

但是,在能够使用PHP mail()函数发送邮件后,我了解到它发送未经验证的电子邮件,这产生了另一个问题。这些邮件都落在了我Hotmail的垃圾邮箱里,有些邮件从来没有发送过,我猜这与它们没有经过认证有关。这就是为什么我决定从mail()转换到使用SMTP的PHPMailer。

其他回答

我觉得这个应该能搞定。我只是添加了一个if(isset,并在主体中的变量中添加了连接,以将PHP与HTML分开。

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com'; 
    $to = 'contact@yoursite.com'; 
    $subject = 'Customer Inquiry';
    $body = "From:" .$name."\r\n E-Mail:" .$email."\r\n Message:\r\n" .$message;

if (isset($_POST['submit'])) 
{
    if (mail ($to, $subject, $body, $from)) 
    { 
        echo '<p>Your message has been sent!</p>';
    } 
    else 
    { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }
}

?>

在邮件功能中添加邮件头。

$header = "From: noreply@example.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";

$status = mail($to, $subject, $message, $header);

if($status)
{
    echo '<p>Your mail has been sent!</p>';
} else {
    echo '<p>Something went wrong. Please try again!</p>';
}

您可以使用CodeIgniter配置电子邮件。例如,使用SMTP(简单方式):

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'mail.domain.com', // Your SMTP host
        'smtp_port' => 26, // Default port for SMTP
        'smtp_user' => 'name@domain.com',
        'smtp_pass' => 'password',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
);
$message = 'Your msg';
$this->load->library('email', $config);
$this->email->from('name@domain.com', 'Title');
$this->email->to('emaildestination@domain.com');
$this->email->subject('Header');
$this->email->message($message);

if($this->email->send()) 
{
   // Conditional true
}

这对我很管用!

如果只使用mail()函数,则需要完成配置文件。

您需要打开邮件扩展,并设置SMTP smtp_port等,最重要的是,设置您的用户名和密码。没有它,邮件就无法发送。此外,您还可以使用PHPMail类来发送。

为我解决这个问题的是,一些提供商在使用php邮件时不允许外部收件人:

将代码中的收件人($收件人)更改为本地收件人。这意味着使用来自服务器域的电子邮件地址,例如,如果您的服务器域是www.yourdomain.com,那么收件人的电子邮件地址应该是someone@yourdomain.com。 请上传修改后的php文件,然后重试。 如果仍然不工作:将发件人($sender)更改为本地电子邮件(使用与收件人相同的电子邮件)。 请上传修改后的php文件,然后重试。

希望这能有所帮助! https://www.arclab.com/en/kb/php/how-to-test-and-fix-php-mail-function.html