php空间服务器发送邮件(php群发邮件)
- 服务器
- 2022-12-20 05:16:09
- 12
今天给各位分享php空间服务器发送邮件的知识,其中也会对php群发邮件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
怎么利用php发送邮件求详细教程
PHP虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,接下来将详细介绍,需要了解的朋友可以参考下:
本人使用wamp集成开发环境,Apache2.4.4, Mysql5.6.12 , php5.4.12.开始的时候使用mail()发送邮件,更改配置始终无法成功,了解到mail()函数使用需要sendmail程序。又下载了sendmail程序扩展包。按照网上的说法也改好了php.ini和sendmail.ini。使用foxmail 7.1创建了自己的qq邮箱账户,开启了POP3/SMTP服务,更改发件服务器为POP3,使用和收件服务器相同的身份验证,结果还是报错:Warning: mail(): SMTP server response: 503 Error: need EHLO and AUTH first ! in F:\PHP\wamp\www\mail.php on line 8。以下是使用mail()函数发送邮件的php代码:
[php] view plain copy
span style="font-size:14px"?php
$to = "757006080@qq.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "757006080@qq.com";
$headers = "From: $from";
$send=mail($to,$subject,$message,$headers);
if($send)
echo "Mail Sent";
else
echo "Sorry,mail sent failed!"
?/span
在CSDN论坛上发现phpmailer可以方便快捷的发送邮件,以下写出详细使用教程:
1.需要下载PHPMailer文件包,(点击打开链接)
2.确认你的服务器已经系统支持socket,通过phpinfo()查看是否支持socket;
3.把文件解压到你的WEB服务器目录下,就可以使用PHPMailer发送邮件了。
以下为前台表单php代码:
[php] view plain copy
span style="font-size:14px"html
body
h3phpmailer Unit Test/h3
请你输入font color="#FF6666"收信/font的邮箱地址:
form name="phpmailer" action="testemail.php" method="post"
input type="hidden" name="submitted" value="1"/
邮箱地址: input type="text" size="50" name="to" /
br/
input type="submit" value="发送"/
/form
/body
/html /span
以下为后台程序:
[php] view plain copy
?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require('class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
//$body = file_get_contents('contents.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$to = $_POST['to'];
$mail-CharSet="GB2312";//设置邮件字符编码否则邮件会乱码
$mail-Encoding="base64";
$mail-IsSMTP(); // tell the class to use SMTP
$mail-SMTPAuth = true; // enable SMTP authentication
$mail-Port = 25; // set the SMTP server port
$mail-Host = "smtp.qq.com"; // SMTP server
$mail-Username = "757006080@qq.com"; // SMTP server username
$mail-Password = "000000000000"; // SMTP server password
//$mail-IsSendmail(); // tell the class to use Sendmail
$mail-AddReplyTo("757006080@qq.com","han qing");
$mail-From = "757006080@qq.com";
$mail-FromName = "han qing";
//$to = "hanqing757@gmail.com";
$mail-AddAddress($to);
$mail-Subject =$mail-Subject = "=?utf-8?B?" . base64_encode("First PHPMailer Message") . "?=";
$mail-Body = "h1phpmailer演示/h1 这是用PHPMAILER发的第一份邮件,从QQ邮箱发到Google邮箱.";
$mail-AddAttachment("F:/myloe.jpg");
$mail-AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail-WordWrap = 80; // set word wrap
//$mail-MsgHTML($body);
$mail-IsHTML(true); // send as HTML
$mail-Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e-errorMessage();
}
?
如何在 BlueHost 空间中用 PHP 以 SMTP 方式发送邮件
你可以用 mail,sendmail 和 smtp 三种方式来借助 Mail 类发送邮件。话说几天前刚买了个 BlueHost 的空间,在上面跑程序的时候用 PHP 发邮件搞出点心得来,放在这里跟大家分享。 首先我在系统中添加了一个邮件账户 test,密码 123456,然后写下了如下代码: require_once( 'Mail.php'); $conf = array( 'host' = 'mail.yourdomail.com', 'auth' = true, 'username' = 'test', 'password' = '123456' ); $headers['From'] = 'noreply@yourdomain.com'; $headers['To'] = $To; $headers['Subject'] = '=?GB2312?B?' . base64_encode( $Subject) . '?='; $headers['Content-Type'] = 'text/plain; charset=gb2312'; $Mail = Mail::factory( 'smtp', $conf); $res = $Mail-send( $To, $headers, $Content); ? $To 是想要发送到的信箱地址,比如 foobar@gmail.com,$Subject 是邮件主题,$Content 是 GB2312 编码的邮件正文。结果用以上程序屡试屡败,后来经过多次试验才得出在 BlueHost 主机上用 PHP 以 SMTP 方式发送邮件的正确方法,注意事项如下: 1) BlueHost 帮助信息中说跟你账户(域名)相对应的 SMTP 主机地址是 mail.yourdomain.com,但其实应该是 localhost。你用 mail.yourdomain.com 只会得到 SMTP 拒绝链接的错误信息。 2) SMTP 服务器需要验证才能发邮件。 3) SMTP 验证时用户名需要写 user+yourdomain.com,不能写 user@yourdomain.com。想想这个也好理解,在虚拟主机上一个 SMTP server 要支撑很多个账户,这种写法应该是为了区别发件的账户。 4) 邮件头中的发件邮件账户一定要是存在于系统中的真实账户,象我上面那样注册了一个 test@yourdomain.com,但是发件人账户又写 noreply@yourdomain.com 是不行的,只会得到这样的错误信息: SMTP: Invalid response code received from server (code: 550, response: Verification failed for No Such User Here Sender verify failed) 知道了上面这 4 个注意事项以后就好办了,删掉刚才注册的 test 邮件帐号,注册一个 noreply 帐号,修改程序如下: require_once( 'Mail.php'); $conf = array( 'host' = 'localhost', 'auth' = true, 'username' = 'noreply+yourdomain.com', 'password' = '123456' ); $headers['From'] = 'noreply@yourdomain.com'; $headers['To'] = $To; $headers['Subject'] = '=?GB2312?B?' . base64_encode( $Subject) . '?='; $headers['Content-Type'] = 'text/plain; charset=gb2312'; $Mail = Mail::factory( 'smtp', $conf); $res = $Mail-send( $To, $headers, $Content); ? 邮件被成功的发送出去了,而且由于邮件服务器在美国,发送到 GMail 信箱非常之快,一般这边一发那边刷新一下就能看到新邮件:) 这里再说些题外话,买了 BlueHost 的空间将近一周时间了,感觉美国的虚拟主机市场确实非常成熟,功能强大。跟国内价格还行但是配置超受限的主机比起来性价比高的不得了!只要你选对了服务提供商,可以说绝对让你满意。但是缺点也比较明显,就是速度实在是慢!这里面的原因众所周知,感谢伟大的电信运营商,感谢伟大的 GFW! 今天跟 HostMonster 的客户沟通了一下,问问他们那边从中国过去的访问速度怎么样,估计人家遇到这样的情况不是个别现象,上来就是一句,我们对来自中国的访问速度无法做出承诺。 国内的虚拟主机服务商有万网这样把大家当 SB 漫天要价的主,但是也有兢兢业业踏踏实实做服务的公司,不过性价比跟国外的运营商确实没法比。一边是价格高不了多少,动辄几十 G 空间几百 G 月流量,支持 SSH,支持多 Addon domain 的国外主机,一边是价廉物不太美的国内主机,我想诱惑力是不言而喻的。但是速度,唉,伤心是一种说不出的痛!
php如何发送邮件
你好,用这个邮件类,需要在调用时,填写一个smtp服务器和你的用户名密码。
?php
set_time_limit(600);
/*
* 邮件发送类
*/
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "";
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
/*
* 功能:发信初始化设置
* $from 你的发信服务器上的邮箱
* $password 你的邮箱密码
* $smtp 您的SMTP 服务器供应商,可以是域名或IP地址
* $check SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证
*/
function smail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/", $from ) ) {
$this-username = substr( $from, 0, strpos( $from , "@" ) );
$this-password = $password;
$this-smtp = $smtp ? $smtp : $this-smtp;
$this-check = $check;
$this-s_from = $from;
}
}
/*
* 功能:发送邮件
* $to 目标邮箱
* $from 来源邮箱
* $subject 邮件标题
* $message 邮件内容
*/
function send ( $to, $from, $subject, $message ) {
//连接服务器
$fp = fsockopen ( $this-smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this-check == "1") $lastact="EHLO ".$yourname."\r\n";
else $lastact="HELO ".$yourname."\r\n";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//身份验证
if ($this-check=="1") {
//验证开始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this-username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this-password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: ". $this-s_from . "\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: ". $to ." \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理From头
$head="From: $from\r\n";
$message = $head.$message;
//处理To头
$head="To: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
// 发送示例
// 只需要把这部分改成你的信息就行
$sm = new smail( "用户名", "密码", "发件smtp服务器" );
$end = $sm-send( "收件人", "发件人(可以伪造哦)", "标题", "内容" );
if( $end ) echo $end;
else echo "发送成功!$x";
?
php空间服务器发送邮件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php群发邮件、php空间服务器发送邮件的信息别忘了在本站进行查找喔。
本文由admin于2022-12-20发表在靑年PHP官网,如有疑问,请联系我们。
本文链接:https://www.qnphp.com/post/175199.html