使用 php 发送无需身份验证的 smtp 邮件


Send smtp mail with no authentication using php

我正在尝试使用没有身份验证的smtp从表单发送电子邮件...请帮忙。我打电话给托管公司,他们告诉我,我应该在没有身份验证的情况下这样做。我相信如果我对其进行身份验证会更容易。 这是我在下面编写的代码。

function ProcessForm($values){
include('smtpConfig.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$to = 'mail';
$from = 'fromMail';
$subject = 'Website Contact';
$text     = $values['text'];
$name     = $values['name'];
$email    = $values['email'];  
$body = "
<style>
  * {
    font-family: verdana, helvetia, arial, sans-serif;
    font-size: 14px;
  }

  table {
    width: 600px;
    background:  #fff;
    margin: auto;
    border: #a9ee17 solid 4px;
  }
  table tr td {
    font-family: verdana, helvetia, arial, sans-serif;
    font-size: 14px;
  }
</style>
A message has been sent from site. Its details are as follows:
<br/>
<br/>
<table cellpadding=10 cellspacing=0>
  <tr>
    <th><b>Field</b></th>
    <th><b>Value</b></th>
  </tr>
  <tr class='row'>
    <td><b>Name</b></td>
    <td>$name</td>
  </tr>
  <tr class='row'>
    <td><b>Email Address</b></td>
    <td>$email</td>
  </tr>
  <tr class='row'>
    <td><b>Message</b></td>
    <td>$text</td>
  </tr>
</table>
";
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
echo '<h2 class="center success">Your message was sent. Thank you very much</h2>';
  include_once 'static/contact-details.php';
}
}

下面的 smtpConfig 代码最初设置为接受 stmp 用户名和密码,但由于我尝试在没有任何身份验证的情况下使用 smtp,因此我删除了有关用户名和密码的部分,只留下了 smtpServer 和端口...默认端口号通常为 25,作为默认值,因此我想假设这不是这里的问题。

<?php
//Server Address
$SmtpServer="smtp.host.co.za";
$SmtpPort="25"; //default
class SMTPClient
{
function SMTPClient ($SmtpServer, $SmtpPort, $from, $to, $subject, $body)
{
$this->SmtpServer = $SmtpServer;
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
if ($SmtpPort == "") 
{
$this->PortSMTP = 25;
}
else
{
$this->PortSMTP = $SmtpPort;
}
}
function SendMail ()
{
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
{
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."'r'n"); 
$talk["hello"] = fgets ( $SMTPIN, 1024 );  
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">'r'n"); 
$talk["From"] = fgets ( $SMTPIN, 1024 ); 
fputs ($SMTPIN, "RCPT TO: <".$this->to.">'r'n"); 
$talk["To"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "DATA'r'n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">'r'nFrom: <".$this->from.">'r'nSubject:".$this-       >subject."'r'n'r'n'r'n".$this->body."'r'n.'r'n");
$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ... 
fputs ($SMTPIN, "QUIT'r'n"); 
fclose($SMTPIN); 
// 
} 
return $talk;
 } 
}
?>

代码处理没有错误,但仍然不发送邮件。

您的托管公司正在谈论一个参数

    $mail->SMTPAuth   = MAIL_AUTENTIC;              // enable SMTP autenticacion

MAIL_AUTENTIC应为 True 或 False 才能启用或不启用。在您的情况下 假

一些变量之前已经定义过,例如:

define('MAIL_AUTENTIC', true);      

这里有一个PHPMailer的例子

include('includes/class.phpmailer.php');
$mail = new PHPMailer(true); //  true for exceptions in erors (try/catch)
$mail->IsSMTP();                        
try {                                                       
    $mail->Host       = MAIL_HOST;                  // SMTP server
    $mail->SMTPDebug  = 1;                          // SMTP debug 2 active 1 no active
    $mail->SMTPAuth   = MAIL_AUTENTIC;              // enable SMTP autenticacion
    $mail->Port       = MAIL_PUERTO;                // SMTP port
    $mail->Username   = MAIL_USUARIO;               // SMTP user
    $mail->Password   = MAIL_PASSWORD;              // SMTP password
    $mail->AddReplyTo('ccccc@mydomain.net', 'Envios');          // Reply address
    $mail->AddAddress($TO);
    $mail->SetFrom('Myfrom@mydomain.net', 'Bla bla');
    $mail->AddReplyTo('MyReply@mydomain.net', 'REsponse bla bla');
    $mail->Subject = $SUBJ;
    $mail->AltBody = 'Use HTML';        // optional 
    $mail->CharSet="utf-8";
    $mail->IsHTML(true);
    $mail->MsgHTML($MYTEXT);
    $mail->Send();
} 
catch (phpmailerException $e) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}