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.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况