当我编辑脚本时,PHP 邮件脚本在主机中不起作用


PHP Mail Script not working in Hosting when i edit the script

我遇到了以下问题:

我的网站上有一个小表单,指向一个小型 php 处理器来发送邮件。那里"有效"但不发送邮件。我在其他托管中测试了脚本,就像一个魅力。

我打电话给托管服务提供商,看看问题出在哪里。托管公司提供了以下PHP脚本来测试邮件发送:

<?php
$to = "myemail@myemail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "test@pizanoecheverri.co";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

现在他们告诉我,我做了必要的编辑,所以它可以工作。所以我做到了。我在主页上有这个小表格(你可以在 pizanoecheveri.co 中看到它):

<form name="form1" method="post" action="testm/mailing.php" class="miscclass" id="form1">
<div style="text-align: center;" id="contbaloon"><img border="0" alt="" src="/images/misc/baloon1.png"></div>
<input type="text" name="email" class="contactfield" id="textfield">
<p style="text-align: right;"><input type="submit" value="Enviar" name="button" style="background-color: #bbbdc0; border: 0 none; color: #ffffff; margin-bottom: 17px; margin-top: 10px;" class="contacto-x" id="contactbutton"></p>
</form>

我已经像这样对代码进行了编辑:

<?php
$to = "myemail@myemail.com";
$subject = "Lista de correos.";
$message = "El siguiente correo se debe agregar a la lista de correo. ".$_POST["email"];
$from = $_POST["email"];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
echo $message;
?>

我向托管服务提供商注意到了这一点,他们说这是编程上的问题。这是我不知道的事情,因为我在其他托管中测试了相同的代码并且运行良好。

简而言之,我不明白为什么提供的代码有效,当我编辑代码时,代码在该主机上不起作用,而在其他情况下它工作正常并且没有语法错误,据我和 Dreamweaver 显示。

从该托管中知道的唯一细节是基于Windows运行plesk的,而我测试代码的另一个托管(它在哪里工作)是基于Linux的apache。两者都运行 PHP 5.2.17。

提前致谢

编辑:我测试了以下修改,但在该主机上效果不佳

<?php
$to = "myemail@myemail.com";
$subject = "Lista de correos.";
$message = "El siguiente correo se debe agregar a la lista de correo. ".$_POST["email"];
$from = $_POST["email"];
#$headers = "From:" . $from;
$headers  = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$headers .= 'From: ' . $from . "'r'n";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
echo $message;
?>

请尝试此测试代码。只是复制和害虫。

仅更改标题部分

$headers  = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$headers .= 'From: ' . $from . "'r'n";

问题是因为主机上没有安装 Sendmail,这是一个用于通过 IMAP 协议发送邮件的 Linux 分布式扩展。在现代,也许所有的邮件提供商都禁用处理这些请求,因为不需要有效的发件人来处理请求。

解决方案是使用SMTP协议,该协议也会验证发件人和收件人的电子邮件帐户,因此速度较慢,但更安全。

你不需要知道协议是如何工作的,你可以使用PHP库来做到这一点,即开源的PHPMailer

require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password
    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->Subject = 'Subject';
    $mail->MsgHTML(file_get_contents('contents.html'));
    $mail->AddAttachment('images/phpmailer.gif');
    $mail->Send();
    echo "Message sent!";
} 
catch (PHPMailerException $e) {
    echo $e->errorMessage();
} 
catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
}