简体   繁体   English

为什么我可以使用Swift Mailer从本地主机发送电子邮件,而不能使用PHP的mail()函数发送电子邮件?

[英]Why am i able to send email from localhost using Swift Mailer but not with PHP's mail() function?

I am working on a MAMP PRO localhost server. 我正在MAMP PRO本地服务器上工作。 I am very sure emails were being sent some months ago on my localhost from my local applications using php's mail() function. 我非常确定几个月前使用php的mail()函数从本地应用程序在本地主机上发送了电子邮件。 All of a sudden i can't get emails to be sent. 突然我无法收到要发送的电子邮件。 I have even moved one of the local websites to a live server on GoDaddy but emails are still not being sent. 我什至将一个本地网站移到GoDaddy上的实时服务器上,但仍未发送电子邮件。

The GoDaddy guys swear to that nothing is wrong with their settings and that the issue has to be in my code. GoDaddy成员发誓他们的设置没有问题,并且问题必须出在我的代码中。 Getting back to my localhost MAMP PRO server, i tried all i could to debug, including messing around with the php.ini settings to no avail. 回到我的本地主机MAMP PRO服务器,我尝试了所有可能的调试,包括弄乱php.ini设置无济于事。 I finally reluctantly decided to try an email library, so i went for Swift Mailer. 我最终很不情愿地决定尝试一个电子邮件库,因此我选择了Swift Mailer。 I installed it, wrote the script and now emails are being sent from localhost. 我安装了它,编写了脚本,现在从本地主机发送电子邮件。 I assume it will also work if i put the code on the live server (at GoDaddy). 我认为如果我将代码放在实时服务器(在GoDaddy)上,它也将起作用。

   Here is my mail() code:
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
    $headers .= "From: ". "$this->_headerFrom". "\r\n";
    $headers .= "Reply-To: "."$email". "\r\n";
    $headers .= "Return-Path: "."$email". "\r\n";
    $headers .= "X-Mailer: PHP/".phpversion();

    $to = "$this->_applicationEmail";

    $send = mail($to, $subject, $msg, $headers);

$subject and $msg are all set earlier in the script and they are strings
$send returns true, but no email is sent. I have followed all sorts of 
examples here on SO to no avail. It's simple, but mind-boggling all the 
same. 

I really need to understand why. 我真的需要明白为什么。 What is Swift Mailer doing that mail() isn't? Swift Mailer在做什么,而mail()不是? I would rather keep my mail() scripts if only i could get them to work. 如果只有我可以让他们工作,我宁愿保留我的mail()脚本。 The fact is, i had spent a lot of time writing custom email scripts with several email templates. 事实是,我花了很多时间用几个电子邮件模板编写自定义电子邮件脚本。 It would be a heck of a task to rewrite all those mailing scripts to use Swift Mailer. 重写所有这些邮件脚本以使用Swift Mailer将会是一件艰巨的任务。 What could make mail() stop working? 什么会使mail()停止工作?

MAMP/WAMP need to be set up correctly to send mail. 需要正确设置MAMP / WAMP以发送邮件。

See http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/ . 请参阅http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/

The same for your GoDaddy server you probably need to do something with SMTP to make sure it is set up correctly. 对于您的GoDaddy服务器,您可能还需要对SMTP进行某些操作以确保正确设置。

You might need to do a few tests to see if mail is actually getting sent or just get flat out rejected as spam by the server receiving it as it won't have SPF etc,. 您可能需要进行一些测试,以查看邮件实际上是在发送还是只是被服务器接收,因为它没有SPF等被拒绝为垃圾邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM