I'm trying to add the feature to send "receipts" after buying and selling stocks. It seems to be very straightforward, I'm just using the code that David used in froshims, but I get the following error:

The following From address failed: [email protected] : Called Mail() without being connected

It seems that it is not connecting. Does this have to do with me not modifying the host?

// send user confirmation via email

// instantiate mailer
$mail = new PHPMailer();

// use your ISP's SMTP server (e.g., smtp.fas.harvard.edu if on campus or smtp.comcast.net if off campus and your ISP is Comcast)
$mail->Host = "smtp.fas.harvard.edu";

// set From:
$mail->SetFrom("[email protected]");

// set To:

// set Subject:
$mail->Subject = "C$50 Finance: Stocks";

// set body 
$mail->Body = "Hello" . $username . ", you just bought" . $shares . "of" . $stock["name"] . "at" . $stock["price"] . "per share.";
// send mail
if ($mail->Send() === false)
    die($mail->ErrorInfo . "\n");
  • Can i assume that you are not on Harvard campus?
    – Gorozhin
    Sep 9, 2014 at 9:34

1 Answer 1


The problem is really about host(smtp.fas.harvard.edu),

I had the same problem and here is my decision:

I created a new mailbox at gmail, and used it like this:

/*Sends a message of $body with subject $subj to $to*/
function email($to, $subj, $body)
    // create mail object and set some settings settings
    $mail = new PHPMailer();
    $mail->SMTPAuth   = true;//required for gmail
    $mail->Host = "smtp.gmail.com";//required for gmail
    $mail->Username   = "";//your new login to gmail
    $mail->Password   = "";//your new password to gmail
    $mail->SMTPSecure = "tls"; //required for gmail
    $mail->Port = 587;//required for gmail
    $mail->SetFrom("");//your new login to gmail

    $mail->Subject = $subj;
    $mail->Body = $body;
    $mail->IsHTML(true);//I used html pre formatted letters
    return $mail->Send() === false; //returns false in case of mistake


function uses gmail server to deal with letters

for easier usage you can add it to the end of includes/functions.php

  • aikrikunov95 I made the same code as you wrote but it continuously gives error: the following from address failed. can you help me . my code is the same as yours
    – mojtaba1
    Jul 12, 2015 at 18:13
  • mojtaba1 did you actually followed the comments and filled in your email info?
    – Gorozhin
    Jul 13, 2015 at 19:50
  • I added the function you have suggested above to the helpers.php and this line to the buy.php in public. mail($cash_portfolio[0][... // send email render("buy_confirmation.p... //buy confirmation view Everything runs but I dont receive any email or any error message. Is this path require_once("libphp-phpmailer/class.phpmailer.php"); correct?
    – monkey_tan
    Apr 4, 2016 at 7:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .