本文目錄一覽:
- 1、php怎麼通過郵件的方式發送一封URL帶參數的地址的郵件?
- 2、如何通過一個php程序給不同的郵箱發送不同的郵件
- 3、PHP中發送郵件的代碼!
- 4、php發送郵件的問題:php怎麼才能發送郵件呢?使用自帶的函數,應該怎樣設置相應的郵件服務器?
- 5、PHP發送的電郵, hotmail收不到, 怎樣做?
php怎麼通過郵件的方式發送一封URL帶參數的地址的郵件?
思想:
通過str_replace來替換掉參數的位置。
類似問題:
小明現在需要發送帶參數$code的url ——”” ,但是直接輸出就是純粹的$code。
解決方法:
僅供參考
//設$content 為郵件內容
$content = EOF
p align=”center”a href=”Click me to get yoghurt!/abr
/p
EOF;
//設$str 為被替換的字符串
//設$code 為替換字符串,現在要替換掉$content中的”$code”為真正的變量 $code。
$str= ‘$code’;
$code = $_GET[‘code’];
$content = str_replace($str,$code,$content);
//最終就可以實現發送一封URL帶參數郵件的效果了
總結:
此種情況的確比較特殊,但我們平常還是應該多補習一下PHP的函數,在實戰中發揮。
關於我:
MarikoChiba,星雲茶館的站長,歡迎各位的光臨。
如何通過一個php程序給不同的郵箱發送不同的郵件
1.需要一個用來發送電子郵件的文件的程序,也就是一個php文件,流行的phpmail有很多,今天以smtp.php為例演示。
2.將其複製到你的項目文件內,具體路徑根據自己的實際情況,這裡建了一個test文件,用來設置郵件參數,大家也可以自定義郵件參數文件。(PS:一般都是通過表單接受的)
3.smtp.php文件引入。
4.設置郵件參數,具體代碼如下:
//引入發送郵件類
require(“smtp.php”);
$smtpserver = “smtp.163.com”;
$smtpserverport = 25;
//你的163服務器郵箱賬號
$smtpusermail = “@163.com”;
//收件人郵箱
$smtpemailto = “@qq.com”;
//你的郵箱賬號(去掉@163.com)
$smtpuser = “”;//SMTP服務器的用戶帳號
//你的郵箱密碼
$smtppass = “”; //SMTP服務器的用戶密碼
5.設置郵件內容,代碼如下:
//郵件主題
$mailsubject = “測試郵件發送”;
//郵件內容
$mailbody = “PHP+MySQL”;
//郵件格式(HTML/TXT),TXT為文本郵件
$mailtype = “TXT”;
//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
//是否顯示發送的調試信息
$smtp-debug = TRUE;
//發送郵件
$smtp-sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
刷新頁面後郵件發送,參數不出錯的話1分鐘左右即可發送成功。
至於發幾個人,如何發自己定義下就行
PHP中發送郵件的代碼!
從代碼來看,想必樓主是想把表單提交的內容發送至指定郵箱,而這個郵箱地址也是在表單中填寫的,$sendTo變量的值被賦為表單項“toMail”傳過來的值。
如此,你可以在填寫表單的時候在toMail表單項里填入“88888@qq.com”,或者直接$sendTo=”88888@qq.com”。
接下來就要考慮是否能發送出去了:
1、如果樓主配置的服務器安裝了sendmail組件,就可以直接使用你寫的這段代碼來完成任務。
2、如果沒有安裝sendmail組件,就要接用smtp服務器來發送了。這樣相對較複雜,要引用一個PHP類。具體代碼如下:
SMTP.PHP:
?php
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”);
}
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 CRLF.CRLF [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(“^.+@([^@]+)$”, “\1”, $address);
if (!@getmxrr($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 (!@file_exists($this-log_file) || !($fp = @fopen($this-log_file, “a”)))
{
$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;
}
}
}
?
DEMO:TEST.PHP
?php
require “smtp.php”;
$smtpserver = “smtp.xxx.com”;//SMTP服務器
$smtpserverport =25;//SMTP服務器端口
$smtpusermail = “xxxx@xxx.com”;//SMTP服務器的用戶郵箱
$smtpemailto = “xxxx@xxx.com”;//發送給誰
$smtpuser = “xxxx”;//SMTP服務器的用戶帳號
$smtppass = “xxxx”;//SMTP服務器的用戶密碼
$mailsubject = “客戶反饋意見!”;//郵件主題
$mailbody = “h1這是我的一個測試~~~/h1”;//郵件內容
$mailtype = “HTML”;//郵件格式(HTML/TXT),TXT為文本郵件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證.
$smtp-debug = false;//是否顯示發送的調試信息
$return = ($smtp-sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype));
echo $return;
?
php發送郵件的問題:php怎麼才能發送郵件呢?使用自帶的函數,應該怎樣設置相應的郵件服務器?
首先,我不想給phpmailer這個東西做廣告,但是我確實使用的這個東西,很費解。
下載一個phpmailer類,裡面主要的是三個文件class.phpmailer.php\class.pop3.php\class.smtp.php
將這三個文件放到同一個文件夾.eg:papmailer
申請一個網絡郵件服務器(我用的是163服務器)
編寫發送郵件代碼如下
/**
* 發送郵件
* @param unknown_type $sendto_email 接收人Email
* @param unknown_type $subject 郵件主題
* @param unknown_type $body 郵件內容
* @param unknown_type $user_name 接受人姓名
*/
function send_email( $sendto_email, $subject, $body, $user_name){
require_once “phpmailer/class.phpmailer.php”;
$mail = new PHPMailer(); //創建類對象
$mail-IsSMTP();
$mail-Host = “smtp.163.com”; // SMTP servers 使用163服務器,郵件smtp服務器
$mail-SMTPAuth = true; // turn on SMTP authentication
$mail-Username = “你申請的163郵箱帳號”; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail-Password = “163郵箱密碼”; // SMTP password
$mail-From = “發件人郵箱,可以使用上面163郵箱”; // 發件人郵箱
$mail-FromName = “發件人名稱,自己隨意命名”; // 發件人
$mail-CharSet = “utf8”; // 這裡指定字符集!
$mail-Encoding = “base64”;
$mail-AddAddress($sendto_email,$user_name); // 收件人郵箱和姓名
$mail-IsHTML(true); // send as HTML
// 郵件主題
$mail-Subject = $subject;
// 郵件內容
$mail-Body = $body;
$mail-AltBody =”text/html”;
if(!$mail-Send()){
return $mail-ErrorInfo;
}else {
return true;
}
}
PHP發送的電郵, hotmail收不到, 怎樣做?
親 我給你份資料,你自己學學 關於 PHPmailer的,這個是燕十八的課,你網上搜搜,他的網站 自學IT網
,不行你就用他的方法 發郵件 真的自己使用socke他純便 指不定就那裡寫錯了,建議你使用PHPMAILER
第一,需要下載PHPMailer文件包phpmailer.
第二,確認你的服務器系統已經支持socket ,通過phpinfo();查看是否支持sockets(socket 是屬於PHP擴展部分),如果顯現為“enabled”,那就是支持了。
第三,把文件解壓到你的web服務器目錄下,調用類就可以了.
首先包含class.phpmailer.php,然後創建對象,設置參數,調用成員函數。
例1,做成函數方便調用
複製代碼 代碼如下:
?php
require(“phpmailer/class.phpmailer.php”);
function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
$mail = new PHPMailer();
$mail-IsSMTP(); // send via SMTP
$mail-Host = “200.162.244.66”; // SMTP servers
$mail-SMTPAuth = true; // turn on SMTP authentication
$mail-Username = “yourmail”; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail-Password = “mailPassword”; // SMTP password
$mail-From = “yourmail@yourdomain.com”; // 發件人郵箱
$mail-FromName = “管理員”; // 發件人
$mail-CharSet = “GB2312”; // 這裡指定字符集!
$mail-Encoding = “base64”;
$mail-AddAddress($sendto_email,”username”); // 收件人郵箱和姓名
$mail-AddReplyTo(“yourmail@yourdomain.com”,”yourdomain.com”);
//$mail-WordWrap = 50; // set word wrap 換行字數
//$mail-AddAttachment(“/var/tmp/file.tar.gz”); // attachment 附件
//$mail-AddAttachment(“/tmp/image.jpg”, “new.jpg”);
$mail-IsHTML(true); // send as HTML
// 郵件主題
$mail-Subject = $subject;
// 郵件內容
$mail-Body = ”
htmlhead
meta http-equiv=”Content-Language” content=”zh-cn”
meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″
/head
body
I love php。
/body
/html
“;
$mail-AltBody =”text/html”;
if(!$mail-Send())
{
echo “郵件發送有誤 p”;
echo “郵件錯誤信息: ” . $mail-ErrorInfo;
exit;
}
else {
echo “$user_name 郵件發送成功!br /”;
}
}
// 參數說明(發送到, 郵件主題, 郵件內容, 附加信息, 用戶名)
smtp_mail(“yourmail@yourdomain.com”, “歡迎使用phpmailer!”, “NULL”, “yourdomain.com”, “username”);
?
注意:
1. 郵件的字符集設置, $mail-CharSet = “GB2312”; // 這裡指定字符集!在這裡我只指定為GB2312因為這樣Outlook能正常顯示郵件主題,我嘗試過設為utf-8但在Outlook下顯示亂碼。
2. 如果是發送html格式的郵件,那麼記得也指定
3. 如果你想用它來群發郵件的話,記得修改包含文件函數,如:
require(“phpmailer/class.phpmailer.php”);
改為
require_once(“phpmailer/class.phpmailer.php”);
否則的話會產生類的重定義。
個人認為要使用phpmailer,首先,需要有一個郵件服務器,PHP的 mail函數沒有指定,應該是使用的PHP設置的SMTP。
而在這裡需要具體指定,同時需要指定郵件服務器的管理者和密碼。
PHPMailer 也是一個功能強大的郵件類
PHPMailer的主要功能特點:
支持郵件 s/mime加密的數字簽名
支持郵件多個 TOs, CCs, BCCs and REPLY-TOs
可以工作在任何服務器平台,所以不用擔心WIN平台無法發送郵件的問題的
支持文本/HTML格式郵件
可以嵌入image圖像
對於郵件客戶端不支持HTML閱讀的進行支持
功能強大的發送郵件調試功能debug
自定義郵件header
冗餘SMTP服務器支持
支持8bit, base64, binary, and quoted-printable 編碼
文字自動換行
支持多附件發送功能
支持SMTP服務器驗證功能
在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平台測試成功
提供的下載文件中,包括內容詳細的說明文檔及示例說明,所以不用擔心難於上手的問題!
PHPMailer 非常小巧、簡單、方便、快捷
以上資料由Jiucool 翻譯自phpmailer 官網,轉載請註明!
PHPMailer的使用(這裡以使用gmail smtp發送郵件為例,當然也支持sendmail pop 等其他方式):
首先到 下載最新版本的程序包
下載完成後,找到class.phpmailer.php 、class.smtp.php兩個類放到自己的目錄下!
然後新建一個php文件這裡命名為:phpmail_jiucool.php
phpmail_jiucool.php內容如下:
我直接將郵件發送模塊寫成一個函數postmail_jiucool_com(),大家使用的時候直接調用該函數即可,函數內容為:
複製代碼 代碼如下:
function postmail_jiucool_com($to,$subject = “”,$body = “”){
//Author:Jiucool WebSite:
//$to 表示收件人地址 $subject 表示郵件標題 $body表示郵件正文
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set(“Asia/Shanghai”);//設定時區東八區
require_once(‘class.phpmailer.php’);
include(“class.smtp.php”);
$mail = new PHPMailer(); //new一個PHPMailer對象出來
$body = eregi_replace(“[\]”,”,$body); //對郵件內容進行必要的過濾
$mail-CharSet =”UTF-8″;//設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼
$mail-IsSMTP(); // 設定使用SMTP服務
$mail-SMTPDebug = 1; // 啟用SMTP調試功能
// 1 = errors and messages
// 2 = messages only
$mail-SMTPAuth = true; // 啟用 SMTP 驗證功能
$mail-SMTPSecure = “ssl”; // 安全協議
$mail-Host = “smtp.googlemail.com”; // SMTP 服務器
$mail-Port = 465; // SMTP服務器的端口號
$mail-Username = “SMTP服務器用戶名”; // SMTP服務器用戶名
$mail-Password = “SMTP服務器密碼”; // SMTP服務器密碼
$mail-SetFrom(‘發件人地址,如admin#jiucool.com #換成@’, ‘發件人名稱’);
$mail-AddReplyTo(“郵件回復地址,如admin#jiucool.com #換成@”,”郵件回復人的名稱”);
$mail-Subject = $subject;
$mail-AltBody
= “To view the message, please use an HTML compatible email viewer! –
From “; // optional, comment out and test
$mail-MsgHTML($body);
$address = $to;
$mail-AddAddress($address, “收件人名稱”);
//$mail-AddAttachment(“images/phpmailer.gif”); // attachment
//$mail-AddAttachment(“images/phpmailer_mini.gif”); // attachment
if(!$mail-Send()) {
echo “Mailer Error: ” . $mail-ErrorInfo;
} else {
echo “Message sent!恭喜,郵件發送成功!”;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237333.html