1

I am developing a PHP application locally in a mac. I need to develop functionalities where I would need to send emails on certain scenarios. In order to develop and test, I did some research on how to do this in MAC/XAMPP.

For the development purpose I want to use existing resources in MAC/XAMPP rather than third party ones. Hoping in live all in need to do is change the configuration and code works fine using hosting email infrastructure.

Can you suggest how to do this?

(I do hear about postfix but could not figure out how to configure this?)

user4826346
  • 23
  • 1
  • 1
  • 4

4 Answers4

0

I think This Answer will help you to configure Your Email Settings on XAMPP.

How to configure XAMPP to send mail from localhost?

Community
  • 1
  • 1
Manohar Kumar
  • 605
  • 9
  • 12
0

You can use this PHP script for sending an email, when you run mail(...);.

<?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);

?>

See the Documentation.

WuerfelDev
  • 140
  • 1
  • 13
0

I once had this problem on my development machine. Rather then trying to configure SMTP correctly, I asked another server to do the job for me. You can use cURL library to post the required fields ($from, $to, $body etc), and the corresponding script on the remote machine will do the email for you.

local machine code:

<?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");

remote machine code: (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"
}
IT goldman
  • 23
  • 5
0

The thing is that mail() didn't work on Xampp before but since I updated to Xampp 5.6.3 (on mac) it suddenly did. But not all emails would receive it. My mail on gmx.net wouldn't accept the mails but my mail addresses connected to my webhotel did.

But I use phpmailer https://github.com/PHPMailer/PHPMailer for sending out my mails because when you send out a lot of emails mail() opens and closes the connection on each call but phpmailer can use smtp (for instance you can use gmail, it is slow though) so you can send out many mails in one go. Say, if you are sending out a 1000 mails mail() is not a good choice.

Edit: Example of using phpmailer. (my webhotel also had a smtp server I could use. I just had to ask them about it and get their settings and port no. On my webhotel there were no login requirement but the emails sent should have an email address connected to the webhotel in the from field and it didn't work locally so gmail was the better option although the authentication makes it slow to send an email this way)

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!";
}
broch
  • 433
  • 3
  • 15
  • PHPMailer sounds good. What configuration do I need to do to get PHPMailer working? Do you have a sample code/configuration? – user4826346 May 26 '15 at 13:57
  • I have added an example from this page: http://phpmailer.worxware.com/?pg=examplebgmail – broch May 26 '15 at 14:03