duanbichou4942 2015-12-10 05:30
浏览 81

如何让localhost发送电子邮件? [重复]

This question already has an answer here:

I am unable to send mails through localhost Xampp even after making changes in php.ini and sedmail.php files as per THIS. I have adoubt in sendmail.php file what email & PassWord to give here;

auth_username=   
auth_password=  

Please somebody get me through this.

</div>
  • 写回答

2条回答 默认 最新

  • dsfw2154 2015-12-10 06:05
    关注

    Let we resolve this issue by follow some steps.

    1. Make sure error reporting is enabled and set to report all errors(use code in .php file)

    error_reporting(-1);
    ini_set('display_errors', 'On');
    set_error_handler("var_dump");
    

    2. Check your server's mail logs

    Your localhost should be logging all attempts to send emails through it. The location of these logs will user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.

    3. Make sure you have a mail server set up on localhost

    If you are developing on your local workstation using XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.

    You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.

    You can also use SMTP to send your emails. See this answer to re-check how to do this.

    4. Check to see if mail() returns true or false with message

    Please use below code and let me know what happen. This code will display actual error message and we can resolve this issue.

    Replace

    mail($to, $subject, $message, $headers);
    

    With

    $mailReturn = mail($to, $subject, $message, $headers);
    print('<pre>');
    print_r($mailReturn);
    print('</pre>');
    exit();
    

    If above 3 steps done perfect but no success then follow step 4. Let me know what this code print.

    5. SMTP settings(in php.ini) for send mail from localhost

    If you are using Gmail then you got lucky. Gmail lets us use their SMTP provided that you will have to authenticate it using your own username and password. To use Gmail SMTP the values will be like below:

    //Set the hostname of the mail server
    $mail->Host = "smtp.gmail.com";
    
    //enable this if you are using gmail smtp, for mandrill app it is not required
    //$mail->SMTPSecure = 'tls';
    
    //Set the SMTP port number - likely to be 25, 465 or 587
    $mail->Port = 25;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = "YOUR.ID@GMAIL.COM";
    //Password to use for SMTP authentication
    $mail->Password = "YOUR_PASSWORD";
    

    Or, if you don't want to use your Gmail account then I would suggest to create one account on Mandrill and get your SMTP host, username and password from there. I have tested both gmail and mandrill and they are working pretty good.

    //Set the hostname of the mail server
    $mail->Host = "smtp.mandrillapp.com";
    //Set the SMTP port number - likely to be 25, 465 or 587
    $mail->Port = 25;
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = "YOUR_REGISTER_EMAIL@MANDRILL.COM";
    //Password to use for SMTP authentication
    $mail->Password = "YOUR_PASSWORD";
    

    Make sure all variables value are checked and change if require.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错