<?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代码。 代码完美地提交,但从未发送电子邮件。我该如何解决这个问题?


当前回答

您可以使用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
}

这对我很管用!

其他回答

有几种可能性:

您正面临服务器问题。服务器上没有邮件服务器。所以你的邮件不能工作,因为你的代码是好的,邮件是有类型的。 您没有得到发布的值。尝试使用静态值的代码。 使用SMTP邮件发送邮件…

首先,mail()函数的参数可能太多了…… 你最多可以有五个,邮件(到,主题,消息,标题,参数);

至于$from变量,如果你使用Linux cPanel,它应该自动来自你的webhost。它自动来自你的cPanel用户名和IP地址。

$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";

还要确保mail()函数中变量的顺序正确。

将邮件($to、$subject、$message等)按此顺序发送,否则有可能无法工作。

可能问题出在邮件服务器的配置上。为了避免这类问题,或者您不必担心邮件服务器的问题,我建议您使用PHPMailer。

它是一个插件,拥有发送邮件所需的一切,唯一需要考虑的是启用SMTP端口(端口:25和465)。

require_once 'PHPMailer/PHPMailer.php';
require_once '/servicios/PHPMailer/SMTP.php';
require_once '/servicios/PHPMailer/Exception.php';

$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'correo@gmail.com';
    $mail->Password = 'contrasenia';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    // Recipients
    $mail->setFrom('correo@gmail.com', 'my name');
    $mail->addAddress('destination@correo.com');

    // Attachments
    $mail->addAttachment('optional file');         // Add files, is optional

    // Content
    $mail->isHTML(true);// Set email format to HTML
    $mail->Subject = utf8_decode("subject");
    $mail->Body    = utf8_decode("mail content");
    $mail->AltBody = '';
    $mail->send();
}
catch (Exception $e) {
    $error = $mail->ErrorInfo;
}

您可以使用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
}

这对我很管用!

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

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

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