doupa8922 2018-08-15 07:24 采纳率: 100%
浏览 91

使用php的默认mail()函数与office365 [重复]

This question already has an answer here:

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');
}
</div>
  • 写回答

1条回答 默认 最新

  • douao8204 2018-08-15 08:13
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效