0

Is there a way to set the SMTP settings in php.ini, or somewhere else, to get the default mail() function to work with my office365 mail account? I prefer not to use API's like the PHPMailer.


The problem with PHP mailer is that it won't send out of the domain the website is hosted on, the error I get is: "Could not instantiate mail function."

Apart from that PHPmailer works fine, any idea how to fix this? When I change the "$to" line, I'll get the error.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../../lib/src/Exception.php';
require '../../lib/src/PHPMailer.php';
require '../../lib/src/SMTP.php';

$to = 'info@***.nl';
$subject = 'PHPMailer GMail SMTP test';
$message = "test body";

$mailer = new PHPMailer;
//$mailer->isSMTP(); 
$mailer->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mailer->Host = "smtp.office365.com"; // use $mailer->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mailer->Port = ***; // TLS only
$mailer->SMTPSecure = 'tls'; // ssl is depracated
$mailer->SMTPAuth = true;
$mailer->Username = '***';
$mailer->Password = '***';
$mailer->setFrom('noreply@***.nl','**',0);
$mailer->addAddress($to, '**');
$mailer->addReplyTo('info@**.nl','**');
$mailer->Subject = $subject;
$mailer->msgHTML($message); //$mailer->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mailer->AltBody = 'HTML messaging not supported';
// $mailer->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mailer->send()){
    //echo "Mailer Error: " . $mailer->ErrorInfo;
    echo 0;
}else{
    echo 1;
    //$_SESSION['mailerfeedback'] = "Bedankt voor je bericht!";
    //header('Location: contact.php');
}
Jeroen
  • 241
  • 3
  • 21
  • you need setup sendmail on server, and configure him by smtp settings. Sorry for my English. – Naumov Aug 15 '18 at 07:28

1 Answers1

3

Are you using localhost server or host server?

If you are running host server, please contact your host server provider.

If you are running on localhost server, make sure you have localhost mail server on your machine as well. Mail server could connect to your SMTP server.

To configure, open your php.ini file, search for [mail function], change the details of your mail server. This chould be a local mail server or the mail server of your ISP.

[mail function]
SMTP = mail.yourserver.com
smtp_port = 25
auth_username = smtp-username
auth_password = smtp-password
sendmail_from = you@yourserver.com

After saving php.ini, then restart your server. Then log into your mail account, go to setting, and enable IMAP.

If you don't have mail server on your local machine, then a good idea is to download available libraries, such PHPMailer, PEAR, Fake sendmail...There are many resources that show you how to set up and configure local mail server.

Sophia
  • 43
  • 6
  • The problem with PHPMailer is that, when I send out of the domain where the website is hosted on, it gives an error: "Could not instantiate mail function.". Apart from that PHPMailer would be fine. Any idea's how to fix this? (i'll add my code above) – Jeroen Aug 15 '18 at 08:56
  • I forgot; it is running on a host server. – Jeroen Aug 15 '18 at 09:10
  • Try to inistiallize this way: $mailer->isSMTP(); – Sophia Aug 15 '18 at 09:10
  • The error "Could not instantiate mail function" means that you don't have a local mail sever configured, i.e. it's not a problem with PHPMailer. SMTP is definitely a much better way to go (it's faster & more secure), but requires that your ISP allows outbound remote SMTP, which many do not. – Synchro Aug 15 '18 at 09:53