从php本地主机发送电子邮件
问题描述:
好吧,我只是不知道使用PHP脚本从本地主机发送电子邮件的方式和工具的数量。从php本地主机发送电子邮件
你们能帮我清理一些东西吗?
这是我的操作类型:在php.ini
- 我启用了Gmail SMTP,设置端口465
- 我已经下载PHPMailer的,按照指令进行测试没有成功。
- 我有远程服务器邮件配置(我的网站以前托管)。
- 此外,我有一个从互联网下载的PHP脚本应该发送电子邮件。
究竟需要从本地主机使用PHP脚本发送电子邮件?
答
正如我所看到的,你使用Gmail作为smtp 那么你需要配置php邮件程序脚本......如果你编辑你的答案并把代码放在那里,这将很好。
之后,你需要检查的权限,文件权限/服务器的权限/ Gmail的权限
答
你需要从DNS MX主机名得到你想要发送电子邮件 ([email protected]域 - >域嘘声。 XX):
function getMX($hostname = "boo.xx", $show = 0){
if(dns_get_mx($hostname, $mxhosts, $weights)) {
$i = 0;
$mxList = NULL;
foreach($mxhosts as $key => $host) {
if($show == 1) echo "Hostname: $host (Weight: {$weights[$key]}) <br>";
$ip = gethostbyname($host);
if($show == 1) echo "IP " . $ip . "\n<br>";
if($show == 1) echo "IP " . gethostbyaddr($ip) . "\n<br>";
$mxList[$i]['host'] = $host;
$mxList[$i]['ip'] = $ip;
$mxList[$i]['weight'] = $weights[$key];
$i++;
}
return $mxList;
} else {
echo "Could not find any MX records for $hostname\n";
}
}
现在你有一个MX主机列表中,那么你需要:
将电子邮件发送到端口25这个主机(总是端口25和无认证)与PHPMailer的或套接字客户端(例如使用SSL/tls支持和au用户鉴定):
<?php
// Send with smtp ssl
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");
// Login email and password
$login = "[email protected]";
$pass = "123456";
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
// echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$socket) {
print "Failed to connect $err $errstr\n";
return;
}else{
// Http
// fwrite($socket, "GET/HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
// Smtp
echo fread($socket,8192);
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
// Start tls connection
echo fwrite($socket, "STARTTLS\r\n");
echo fread($socket,8192);
echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
// Send ehlo
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
// echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
// echo fread($socket,8192);
echo fwrite($socket, "AUTH LOGIN\r\n");
echo fread($socket,8192);
echo fwrite($socket, base64_encode($login)."\r\n");
echo fread($socket,8192);
echo fwrite($socket, base64_encode($pass)."\r\n");
echo fread($socket,8192);
echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
echo fread($socket,8192);
echo fwrite($socket, "DATA\n");
echo fread($socket,8192);
echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
echo fread($socket,8192);
echo fwrite($socket, "quit \n");
echo fread($socket,8192);
/* Turn off encryption for the rest */
// stream_socket_enable_crypto($fp, false);
fclose($socket);
}
}catch(Exception $e){
echo $e;
}
,无认证
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
// echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
echo $socket = stream_socket_client('tcp://mxhost.boo.xx:25', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$socket) {
print "Failed to connect $err $errstr\n";
return;
}else{
// Http
// fwrite($socket, "GET/HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
// Smtp
echo fread($socket,8192);
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
// Start tls connection
echo fwrite($socket, "STARTTLS\r\n");
echo fread($socket,8192);
echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
// Send ehlo
echo fwrite($socket, "EHLO cool.xx\r\n");
echo fread($socket,8192);
echo fwrite($socket, "MAIL FROM: <[email protected]>\r\n");
echo fread($socket,8192);
//echo fwrite($socket, "AUTH LOGIN\r\n");
//echo fread($socket,8192);
//echo fwrite($socket, base64_encode($login)."\r\n");
//echo fread($socket,8192);
//echo fwrite($socket, base64_encode($pass)."\r\n");
//echo fread($socket,8192);
echo fwrite($socket, "rcpt to: <[email protected]>\r\n");
echo fread($socket,8192);
echo fwrite($socket, "DATA\n");
echo fread($socket,8192);
echo fwrite($socket, "Date: ".time()."\r\nTo: <[email protected]>\r\nFrom:<[email protected]\r\nSubject:Hello from php socket tls\r\n.\r\n");
echo fread($socket,8192);
echo fwrite($socket, "quit \n");
echo fread($socket,8192);
/* Turn off encryption for the rest */
// stream_socket_enable_crypto($fp, false);
fclose($socket);
}
}catch(Exception $e){
echo $e;
}
Gmail要求加密连接,它(假设的Windows,因为你已经在php.ini配置这)是不会发生,除非你已经包括了OpenSSL的当你安装PHP的扩展。你做了这个了吗? – DaveRandom 2012-03-07 15:42:43
php.ini中的邮件选项仅适用于Windows主机。 Unix/Linux系统有自己的本地邮件系统,PHP将直接使用。 – 2012-03-07 15:47:47