我在mac本地开发一个PHP应用程序。我需要开发的功能,我需要在某些情况下发送电子邮件。为了开发和测试,我对如何在MAC/XAMPP中做到这一点做了一些研究。
出于开发目的,我想使用MAC/XAMPP中的现有资源,而不是第三方资源。希望在现场所有需要做的是改变配置和代码工作正常使用托管电子邮件基础设施。
你能建议怎么做吗?
(我听说后缀,但不知道如何配置这个?)
问题是mail()之前在Xampp上不起作用,但自从我更新到Xampp 5.6.3 (mac上)之后,它突然起作用了。但并不是所有的邮件都会收到它。我在gmx.net上的邮箱不接受这些邮件,但我在酒店网站上的邮箱地址接受。
但是我使用phpmailer https://github.com/PHPMailer/PHPMailer发送我的邮件,因为当你发送很多电子邮件时,邮件()打开和关闭每次调用的连接,但phpmailer可以使用smtp(例如,你可以使用gmail,它很慢),所以你可以一次发送很多邮件。例如,如果您要发送1000封邮件,mail()不是一个好的选择。
编辑:使用phpmailer的例子。(我的webhotel也有一个smtp服务器可以使用。我只需要问问他们,得到他们的设置和端口号。在我的webhotel上没有登录要求,但是发送的电子邮件应该有一个电子邮件地址连接到webhotel的from字段,它在本地不起作用,所以gmail是更好的选择,尽管身份验证使发送电子邮件的速度变慢)
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[']",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$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!";
}
我想这个答案会帮助你在XAMPP上配置你的电子邮件设置。
如何配置XAMPP从本地主机发送邮件?
当你运行mail(...);
时,你可以使用这个PHP脚本发送电子邮件。
<?php
$receiver = 'receiver_email@example.com';
$subject = 'Did you know...';
$message = "
<html>
<body>
You can use <b>HTML</b> here for formatting the content.<br>Therefore the header has to be set as text/html
</body>
</html>
";
$header = 'MIME-Version: 1.0' . "'r'n";
$header .= 'Content-type: text/html; charset=utf-8' . "'r'n";
$header .= 'From: Example <noreply@example.com>' . "'r'n";
mail($receiver, $subject, $message, $header);
?>
我曾经在我的开发机器上遇到过这个问题。我没有尝试正确配置SMTP,而是要求另一台服务器为我完成这项工作。您可以使用cURL库发送所需的字段($from, $to, $body等),远程机器上相应的脚本将为您发送电子邮件。
本地机器码:
<?php
function curl_post($url, array $post = array(), array $options = array()) {
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 20,
CURLOPT_POSTFIELDS => $post
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch)) {
throw new ErrorException ("curl_post error: " . stripslashes(curl_error($ch)));
}
curl_close($ch);
return $result;
}
function email($from, $to, $subject, $body) {
$result = curl_post ("http://server-with-email/my-email-controller.php", array ("to"=>$to, "from"=>$from, "subject"=>$subject, "body"=>$body));
}
// usage:
$result = email ("from@email.com", "to@email.com", "an email for you", "content of mail");
远程机器码: (my-email-controller.php)
<?php
$to = $_POST["to"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$body = $_POST["body"];
$headers =
"Content-Type: text/plain; charset=UTF-8" . "'r'n" .
"MIME-Version: 1.0" . "'r'n" .
"From: $from" . "'r'n" .
"X-Mailer: PHP/" . phpversion();
if (mail ($to, $subject, $body , $headers)===TRUE) {
echo "mail was sent ok";
} else {
echo "mail failed"
}