我用的是cpanel服务器,我的脚本中有这个类,实际上我不知道他们是如何发送电子邮件的。我在这件事上出错了。我的Smtp课程是
<?php
class Smtp {
var $host = "localhost";
var $port = 25;
var $authenticate = "true";
var $username = "";
var $password = "";
var $smtp = null;
var $connected = false;
var $timeout = 30;
var $error;
var $MAX_CHARS_PER_LINE = 150;
var $NEW_LINE = "\r\n";
var $DEBUG = true;
function send_mail($to, $subject, $message, $from, $charset = "UTF-8", $html = false, $attach = array(), $headers = array()) {
$success = true;
$disconnect_on_exit = false;
$mime_boundary = $this->_generate_boundary();
if (empty($headers)) {
$headers = array();
}
$to = $this->_parse_address($to);
$headers[] = "To: {$to[1]} <{$to[0]}>";
$to_address = $to[0];
$from = $this->_parse_address($from);
$headers[] = "From: {$from[1]} <{$from[0]}>";
$from_address = $from[0];
$headers[] = "Subject: {$subject}";
$headers[] = "Date: " . date("r");
$headers[] = "Message-ID: " . $this->_generate_id();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/mixed;\n\tboundary=\"" . $mime_boundary . "\"";
$message_data = $this->_compose($headers, $message, $charset, $html, $attach, $mime_boundary);
# Connect.
if (!$this->connected) {
$success &= $this->connect();
$disconnect_on_exit = true;
}
if ($this->connected) {
# Send the SMTP commands in sequence.
$success &= $this->_helo();
if ($this->authenticate) {
$success &= $this->_auth();
}
$success &= $this->_mail($from_address);
$success &= $this->_rcpt($to_address);
$success &= $this->_data($message_data);
# All done.
$error = $this->error;
$this->_quit();
if ($disconnect_on_exit) {
$this->disconnect();
}
}
if (!$success) {
$this->error = $error;
}
return $success;
}
function connect() {
$this->smtp = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if (!empty($this->smtp)) {
$reply = $this->_get_reply();
$this->connected = true;
} else {
$this->connected = false;
$this->error = array("Failed to connect.", $errno, $errstr);
$this->_debug($this->error);
}
return $this->connected;
}
function disconnect() {
if (!empty($smtp)) {
fclose($smtp);
$this->connected = false;
}
}
function _compose($headers, $body, $charset, $html, $attach, $mime_boundary) {
$data = "";
# Process headers.
if (is_array($headers) && count($headers) > 0) {
foreach ($headers as $header) {
$data .= $header;
$data .= $this->NEW_LINE;
}
$data .= $this->NEW_LINE;
}
# Normalize new lines.
$body_data = $body;
$body_data = str_replace("\r\n", "\n", $body_data);
$body_data = str_replace("\r", "\n", $body_data);
# Now wrap lines.
$body_data = wordwrap($body_data, $this->MAX_CHARS_PER_LINE, "\n", true);
$body_data = str_replace("\n.", "\n..", $body_data);
# Change new lines back to the required format.
$body_data = str_replace("\n", $this->NEW_LINE, $body_data);
# Add the non-MIME text content.
$data .= "This is a multi-part message in MIME format.";
$data .= $this->NEW_LINE;
# Now add the MIME body part.
$mime_type = $html ? "text/html" : "text/plain";
$data .= $this->NEW_LINE;
$data .= "--{$mime_boundary}";
$data .= $this->NEW_LINE;
$data .= "Content-Type: {$mime_type}; charset=\"{$charset}\"";
$data .= $this->NEW_LINE;
$data .= "Content-Transfer-Encoding: 8bit";
$data .= $this->NEW_LINE;
$data .= $this->NEW_LINE;
$data .= $body_data;
$data .= $this->NEW_LINE;
# Attach the files.
if (is_array($attach)) {
foreach ($attach as $attach_file) {
$attach_filename = str_replace(array("\"", "\r", "\n"), array("-", "-", "-"),
$attach_file['name']);
$attach_mime_type = $attach_file['mime_type'] ? $attach_file['mime_type'] :
"application/octet-stream";
$attach_contents = chunk_split(base64_encode(file_get_contents($attach_file['tmp_name'])));
$data .= $this->NEW_LINE;
$data .= "--{$mime_boundary}";
$data .= $this->NEW_LINE;
$data .= "Content-Type: {$attach_mime_type}; name=\"{$attach_filename}\"";
$data .= $this->NEW_LINE;
$data .= "Content-Transfer-Encoding: base64";
$data .= $this->NEW_LINE;
$data .= "Content-Disposition: attachment; filename=\"{$attach_filename}\"";
$data .= $this->NEW_LINE;
$data .= $this->NEW_LINE;
$data .= $attach_contents;
$data .= $this->NEW_LINE;
}
}
$data .= $this->NEW_LINE;
$data .= "--{$mime_boundary}--";
$data .= $this->NEW_LINE;
return $data;
}
function _helo() {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("HELO {$this->host}", 250);
$result = empty($this->error);
}
return $result;
}
function _auth() {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("AUTH LOGIN", 334);
if (empty($this->error)) {
$reply = $this->_send_command(base64_encode($this->username), 334);
}
if (empty($this->error)) {
$reply = $this->_send_command(base64_encode($this->password), 235);
}
if (!empty($this->error)) {
$this->error[0] = "SMTP error in AUTH";
}
$result = empty($this->error);
}
return $result;
}
function _mail($from) {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("MAIL FROM:<{$from}>", 250);
$result = empty($this->error);
}
return $result;
}
function _rcpt($to) {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("RCPT TO:<{$to}>", array(250, 251));
$result = empty($this->error);
}
return $result;
}
function _data($data) {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("DATA", 354);
if (empty($this->error)) {
$command = "{$data}{$this->NEW_LINE}{$this->NEW_LINE}.";
$reply = $this->_send_command($command, 250);
}
if (!empty($this->error)) {
$this->error[0] = "SMTP error in DATA";
}
$result = empty($this->error);
}
return $result;
}
function _quit() {
$result = false;
if ($this->connected) {
$reply = $this->_send_command("QUIT", 221);
$result = empty($this->error);
}
return $result;
}
function _send_command($command, $success_code = null) {
$error = null;
if ($this->connected) {
$this->_debug($command);
fputs($this->smtp, "{$command}{$this->NEW_LINE}");
$reply = $this->_get_reply();
$code = substr($reply, 0, 3);
$message = substr($reply, 3);
if (!empty($success_code)) {
$success = (is_array($success_code))
? in_array($code, $success_code)
: ($code == $success_code);
if (!$success) {
$command_name = $this->_first_word($command);
$error = array("SMTP error in {$command_name}", $code, $reply);
}
}
}
$this->error = $error;
return array("code" => $code, "message" => $message, "error" => $error);
}
function _get_reply() {
$reply = "";
while ($line = fgets($this->smtp)) {
$reply .= $line;
if ($line{3} == " ") break;
}
$this->_debug($reply);
return $reply;
}
function _first_word($str) {
list($line) = explode("\n", $str);
list($word) = explode(" ", $line);
return $word;
}
function _debug($message) {
if ($this->DEBUG) {
print_r($message);
echo "\n";
}
}
function _generate_id() {
return generateMessageID();
}
function _generate_boundary() {
return "<<<-=-=-[snet.clf.".md5(time())."]-=-=->>>";
}
function _parse_address($address) {
$parsed_address = null;
if (is_array($address)) {
$parsed_address = $address;
} else {
$match = preg_match("/(.*)(\s+)?<(.*)>/U", $address, $parts);
$parsed_address = $match ? array($parts[3], $parts[1]) : array($address, $address);
}
return $parsed_address;
}
}
if (!function_exists("generateMessageID"))
{
function generateMessageID($prefix="40ftrq") {
$message_id = "<$prefix."
. base_convert((double)microtime(), 10, 36)
. "." . base_convert(time(), 10, 36)
. "@" . $_SERVER['HTTP_HOST'] . ">";
return $message_id;
}
}
我派马修去的是
$smtp = new Smtp();
$smtp->host = "mail.server.com";
$smtp->port = "25"; / testted also 465/587 all port
$smtp->authenticate = "true";
$smtp->username = "robot@server.com";
$smtp->password = "password";
$emailer = @sendMail($to, $subj, $msg, $from, $langx['charset']);
echo $debug;
我越来越
220服务器ESMTP Exim 4.91#1星期二,2019 15:04:48 -0400 2203月12日-我们不授权使用此系统来传输未经请求的220和/或批量电子邮件。HELOmail.server.com250服务器你好mail.server.com[xxx.xxx.xxx.xxx]AUTH登录503 AUTH命令在未发布广告时使用MAIL from: 250 OK RCPT TO: 550身份验证失败数据退出
您的邮件服务器不支持身份验证,这意味着它可能会授予您从源IP发送权限,而不是ID和密码。您还使用了一个应该是布尔值的字符串;设置<代码>$smtp-
此外,当您不使用PHPMailer时,请不要用PHPMailer
标记问题-尽管也许您应该这样做,而不是重新发明轮子?
我正在尝试使用javamail和smtp发送邮件。当我从我的gmail地址发送邮件时,它对我有效。我改变了从我的交换账户发送的相同代码,但它不起作用。我正在获得身份验证失败异常。 密码 我尝试使用默认端口,即25,但那一次出现了错误,显示了错误的端口号。所以我假设端口号是对的。我尝试了ping主机和它的工作。我尝试的用户名。但总是有同样的例外。请帮我修一下。 提前谢谢 我启动了调试,得到了以下日志
SMTP服务器 ; [myserverName] 默认用户电子邮件后缀 ; [myemailId] 使用SMTP身份验证 ; true 用户名称 ; [myemailId] 密码 ; [mypassword] 使用SSL ; 假 SMTP
在Wordfence文档中说: 过滤器“wordfence_ls_require_captcha”可用于在您选择的情况下禁用captcha。这对于包含RESTendpoint的插件可能很有用,这些endpoint具有不需要captcha的身份验证。必要时,您的过滤器应返回false以绕过captcha要求,或者在需要CAPTCH时返回true 如果我尝试通过JWT进行API调用,我会通过电子邮件收
这仅适用于(smtp.office365.com)smtp。
我有一个java程序(从谷歌复制的)可以使用office365 SMTP发送电子邮件,它作为一个独立的java程序运行良好,但当我在web应用程序的中将这个java程序部署为jar文件,并从JSP调用该方法时,它抛出了以下错误: 有人能分享一下他们对这个问题的看法吗。java代码: