gpt4 book ai didi

php - 使用php发送没有身份验证的smtp邮件

转载 作者:行者123 更新时间:2023-11-28 08:51:42 25 4
gpt4 key购买 nike

我正在尝试使用没有身份验证的 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;

}

关于php - 使用php发送没有身份验证的smtp邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27312848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com