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

时间:2015-05-26 10:50:20

标签: php macos email xampp

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

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

你能建议怎么做吗?

(我确实听说过postfix但是无法弄清楚如何配置这个?)

4 个答案:

答案 0 :(得分:0)

我认为本答案将帮助您在XAMPP上配置您的电子邮件设置。

How to configure XAMPP to send mail from localhost?

答案 1 :(得分:0)

当您运行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);

?>

请参阅Documentation

答案 2 :(得分:0)

我曾经在我的开发机器上遇到过这个问题。而不是尝试正确配置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"
}

答案 3 :(得分:0)

事情是mail()之前没有在Xampp上工作,但自从我更新到Xampp 5.6.3(在mac上)后突然发生了。 但并非所有电子邮件都会收到它。我在gmx.net上的邮件不接受邮件,但我的邮件地址连接到我的网站。

但我使用phpmailer https://github.com/PHPMailer/PHPMailer发送我的邮件,因为当你发送大量电子邮件时,mail()打开并关闭每次调用的连接,但phpmailer可以使用smtp(例如你可以使用gmail,它虽然很慢)所以你可以一次发送很多邮件。 比方说,如果你发送1000封邮件,邮件()不是一个好的选择。

编辑:使用phpmailer的示例。 (我的webhotel也有一个我可以使用的smtp服务器。我只需要询问它们并获取它们的设置和端口号。在我的webhotel上没有登录要求但是发送的电子邮件应该有一个连接到webhotel的电子邮件地址从字段开始,它没有在本地工作,所以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!";
}