Cannot connenct to relay host 關於傳送email報錯的解決辦法。
給一個朋友做個郵件傳送,居然碰到這問題,多虧老外幫忙。才得以搞定。外國空間
如果你也是用的是以下的程式碼,那麼請把所有的 HELO都改成EHLO儲存試試。。
<?
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file ="";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(/r/n))(//.)", "//1.//3", $body);
$header .= "MIME-Version:1.0/r/n";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html/r/n";
}
$header .= "To: ".$to."/r/n";
if ($cc != "") {
$header .= "Cc: ".$cc."/r/n";
}
$header .= "From: $from<".$from.">/r/n";
$header .= "Subject: ".$subject."/r/n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."/r/n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")/r/n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">/r/n";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."/n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">/n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">/n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host/n");
}
echo "<br>";
echo $header;
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("sending HELO command");
}
}
#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "") {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."/n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."/n");
$this->log_write("Error: ".$errstr." (".$errno.")/n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."/n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^
if ([email protected]($domain, $MXHOSTS)) {
$this->log_write("Error: Cannot resolve MX /"".$domain."/"/n");
return FALSE;
}
foreach ($MXHOSTS as $host) {
$this->log_write("Trying to ".$host.":".$this->smtp_port."/n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("Warning: Cannot connect to mx host ".$host."/n");
$this->log_write("Error: ".$errstr." (".$errno.")/n");
continue;
}
$this->log_write("Connected to mx host ".$host."/n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")/n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."/r/n".$body);
$this->smtp_debug("> ".str_replace("/r/n", "/n"."> ", $header."/n> ".$body."/n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "/r/n./r/n");
$this->smtp_debug(". [EOM]/n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("/r/n", "", fgets($this->sock, 512));
$this->smtp_debug($response."/n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUIT/r/n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned /"".$response."/"/n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."/r/n");
$this->smtp_debug("> ".$cmd."/n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string."./n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (
$this->smtp_debug("Warning: Cannot open log file /"".$this->log_file."/"/n");
return FALSE;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "//([^()]*//)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ /t/r/n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "//1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message."<br>";
}
}
function get_attach_type($image_tag) { //
$filedata = array();
$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);
$filedata['context'] = $image_data;
$filedata['filename']= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
switch($extension){
case ".gif":
$filedata['type'] = "image/gif";
break;
case ".gz":
$filedata['type'] = "application/x-gzip";
break;
case ".htm":
$filedata['type'] = "text/html";
break;
case ".html":
$filedata['type'] = "text/html";
break;
case ".jpg":
$filedata['type'] = "image/jpeg";
break;
case ".tar":
$filedata['type'] = "application/x-tar";
break;
case ".txt":
$filedata['type'] = "text/plain";
break;
case ".zip":
$filedata['type'] = "application/zip";
break;
default:
$filedata['type'] = "application/octet-stream";
break;
}
return $filedata;
}
}
//##########################################
$smtpserver = "smtp.163.com";//SMTP伺服器
$smtpserverport =25;//SMTP伺服器埠
$smtpusermail = "
$smtpemailto = "";//傳送給誰
$smtpuser = "";//SMTP伺服器的使用者帳號
$smtppass = "";//SMTP伺服器的使用者密碼
$mailsubject = "asdfasdf";//郵件主題
$mailbody = "<h1> 這是一個測試程式 </h1>";//郵件內容
$mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文字郵件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
$smtp->debug = true;//是否顯示傳送的除錯資訊
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
?>
相關推薦
Cannot connenct to relay host 關於傳送email報錯的解決辦法。
給一個朋友做個郵件傳送,居然碰到這問題,多虧老外幫忙。才得以搞定。外國空間 如果你也是用的是以下的程式碼,那麼請把所有的 HELO都改成EHLO儲存試試。。 <? class smtp { /* Public Variables */ var $smtp_port;
Cannot connenct to relay host smtp.163.com (php郵件傳送失敗)
真是無語 搞了那麼久,網上全部都說是 伺服器php.ini 配置的問題,全tm都是抄的,誤人子弟,好在 發現 該好貼,不過 阿里雲 也真是 操蛋,遮蔽了25埠也不說,好吧!可能是我 沒注意到。 扯淡完畢,下面是 解決問題的正文: 織夢自帶了郵件傳送功能,當我們在後臺配
python3 通過excel傳送郵件報錯解決辦法
前段時間使用python批量發郵件,通過excel獲取收件人資訊,結果發完後報錯如下:Traceback (most recent call last): File "D:\python practice\傳送郵件 帶附件.py", line 46, in <mod
AFNetworking升級至3.1.0後,原有的方法報錯解決辦法。
為了配合蘋果官方的ipv6的事,有很多開發人員都把AFNetworking升級了(網路請求自己公司做封裝的除外),但是升級後報錯一堆。怎麼辦?我的笨辦法是:一個一個修改。 1.首先:沒有了AFHTTPRequestOperationManager這個類,被AF
Volley網路框架使用過程中的一些報錯解決辦法。
今天第一次開始使用Volley網路框架,的確使用十分便利。大致的使用步驟如下: 1.建立一個RequestQueue物件。 2.建立一個StringRequest或者JsonRequest物件 3.將步驟2建立的物件新增到RequestQueue裡面 相
python selenium chromedriver報錯解決辦法。
提示chromedriver路徑不對的話 直接把chromedriver.exe放到python指令碼的資料夾下面chromedriver下載地址:http://chromedriver.storage.googleapis.com/index.html下載的時候要注意選擇和
從富文字編輯器獲取html內容組裝json,特殊字元引起報錯解決辦法。
最近專案需要,需要從富文字編輯器獲取html內容組裝json,然後還要 把組裝後的json物件利用json2轉成json字串,資料放入編輯器提交,由於相容ie8以上瀏覽器。所以搞了好久的特殊字元轉義,
IntelliJ cannot log in to GitHub上傳github報錯解決
dea cati hub 報錯解決 ima 生成 valid 本地 png 重裝系統,新裝的Intellij IDEA上新建的項目上傳github失敗,報錯: invalid authentication token 。。。 此處多為本地git用戶的用戶名/郵箱
ORA-09817: Write to audit file failed 報錯解決辦法
ora-09817 archivelog 上午開虛擬機登錄數據庫的時候,突然就報錯:ORA-09817: Write to audit file failed寫入失敗?不會是磁盤滿了吧?當然,no space left on device提示的很明顯,df -h 查詢一下,果然100%了:馬上搜羅下o
python 鏈接codis 報錯解決辦法 command 'EXEC' is not allowed
command 'exec' is not allowed python redis codis [[email protected]/* */ ceph]# python ../pkg/redisbase.py Traceback (most recent cal
Authentication token manipulation error報錯解決辦法
erro ive 參考 ext .net center tex tps tro Authentication token manipulation error報錯解決辦法 #參考http://blog.163.com/junwu_lb/blog/static/1916798
Mysql安裝報錯解決辦法
base edi 註意 eve program 搜索 重新啟動 5.1 edit .msi版MySQL安裝包在安裝最後執行的時候到第三部或者第四部死掉 主要是因為之前安裝的版本沒有卸載幹凈,要卸載幹凈MySQ安裝包有一些幾個步驟: 1.通過卸載程序MySQL的相關組件 2。
Maven 本地倉庫明明有jar包,pom文件還是報錯解決辦法
lips rep win 選擇 class osi 方案 pom nbsp 方法一: 找到出錯的jar包文件位置,刪掉_maven.repositories文件 方法二: maven中的本地倉庫的index索引沒有更新導致 解決方案:
loadrunner-27077報錯解決辦法
包含 use msg 報錯解決 運行時 init AC 警告 設置 警告 -27077: “每次叠代模擬一個新用戶”運行時設置為“開”時,“vuser_init”節將包含 Web 函數。這可能會產生具有多次叠代的不可預測結果 [MsgId: MWAR-27077] 正在
3.django連接mysql數據庫及安裝mysqldb驅動報錯解決辦法
64位 分享圖片 l數據庫 required 後來 AS password www com 1.在setting.py設置連接數據庫 DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.m
Rsync服務器搭建遇到的報錯解決辦法
Rsync報錯 linux centOS 遇到錯誤1:[root@backup tmp]# rsync -avz /etc/hosts -e 'ssh -p 22' [email protected]:/tmp/rsync: Failed to exec ssh: No such fi
PHP 結合memcache make 過程的報錯解決辦法
PHP memcache 安裝完LAMP中PHP,後面在PHP結合memcache,換成插件的過程中出現出錯的解決辦法 cd memcache-2.2.1 /application/php5.3.27/bin/phpize ./configure --with-php-config
python2.x下pip install mysql-python報錯解決辦法
分享圖片 alt info pan www. 下載 ima sql pytho 在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 下載該驅動網盤鏈接:https://pan.baidu.com/s/1r0
for迴圈呼叫刪除方法報錯解決辦法
1.每次刪除一個元素,索引往前移,即i-1. List<String> list = new ArrayList<String>(); list.add("111"); &nbs
vue eslint報錯解決辦法
overlay formatter form emit tel erl ORC bubuko 解決辦法 若提示入下圖時,在build / webpack.base.conf.js中, 找到 // const createLintingRule = () => ({//