发送电子邮件-MAC中的PHP XAMPP

发送电子邮件-MAC中的PHP XAMPP,php,macos,email,xampp,Php,Macos,Email,Xampp,我正在mac中本地开发一个PHP应用程序。我需要开发一些功能,在某些情况下我需要发送电子邮件。为了开发和测试,我研究了如何在MAC/XAMPP中实现这一点 出于开发目的,我希望使用MAC/XAMPP中的现有资源,而不是第三方资源。希望在live中,所有需要做的事情就是更改配置,使用托管电子邮件基础设施,代码可以正常工作 你能建议怎么做吗 (我确实听说过postfix,但不知道如何配置它?我认为这个答案将帮助您在XAMPP上配置电子邮件设置 当您运行mail(…)时,您可以使用此PHP脚本发送电子

我正在mac中本地开发一个PHP应用程序。我需要开发一些功能,在某些情况下我需要发送电子邮件。为了开发和测试,我研究了如何在MAC/XAMPP中实现这一点

出于开发目的,我希望使用MAC/XAMPP中的现有资源,而不是第三方资源。希望在live中,所有需要做的事情就是更改配置,使用托管电子邮件基础设施,代码可以正常工作

你能建议怎么做吗


(我确实听说过postfix,但不知道如何配置它?

我认为这个答案将帮助您在XAMPP上配置电子邮件设置


当您运行
mail(…)时,您可以使用此PHP脚本发送电子邮件



请参阅。

我曾经在开发机器上遇到过这个问题。我没有尝试正确配置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");
问题是mail()以前在Xampp上不起作用,但自从我升级到XAMPP5.6.3(在mac上)后,它突然起作用了。 但并不是所有的电子邮件都能收到。我在gmx.net上的邮件不接受这些邮件,但我的邮件地址连接到了我的网络酒店

但是我使用phpmailer发送我的邮件,因为当你发送大量电子邮件时,mail()会在每次通话时打开和关闭连接,但phpmailer可以使用smtp(例如,你可以使用gmail,但速度很慢),所以你可以一次发送多封邮件。 比如说,如果你要发送1000封邮件,mail()不是一个好选择

编辑:使用phpmailer的示例。(我的webhotel也有一个smtp服务器,我可以使用。我只需询问他们,获取他们的设置和端口号。在我的webhotel上,没有登录要求,但发送的电子邮件应该有一个电子邮件地址连接到“发件人”字段中的webhotel,并且它在本地无法工作,因此gmail是更好的选择,尽管身份验证有助于以这种方式发送电子邮件很慢)


PHPMailer听起来不错。要使PHPMailer工作,我需要进行什么配置?你有一个示例代码/配置吗?我在这个页面上添加了一个示例:Windows-那是什么?(开玩笑!)我在MAC环境中,所以这篇文章没有帮助!如果有什么事情对你有帮助,记得设置正确的答案;)因为Stackoverflow也是一款有积分和徽章的游戏,所以我需要设置正确的答案-我是这个网站的新手!
<?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");
<?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"
}
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!";
}