简体   繁体   English

PHPMailer v.mail()用于简单的联系表单

[英]PHPMailer v. mail() for a simple Contact Form

I am new to PHP, but have a decent grasp of things (have not learned classes yet). 我是PHP新手,但对事物有很好的掌握(还没有学过课程)。

The question: 问题:

Which to choose? 选择哪个? PHPMailer or mail() for my new contact form. 我的新联系表单的PHPMailer或mail()。

The form is simple: 表格很简单:

Your name:
Your email:
Subject:
Body:

I have around 2,000 visitors per day and receive about 10 submissions per day, so I don't need anything too fancy. 我每天有大约2,000名访客,每天收到大约10份提交,因此我不需要任何太过花哨的东西。 =) =)

Miscellaneous questions in my head: 我头脑中的其他问题:

  • Is PHPMailer going to better protect my Contact Form from CC: injection (major concern)? PHPMailer是否会更好地保护我的联系表格免受CC:注入(主要关注)? I already know the anti-spambot display:none CSS trick. 我已经知道anti-spambot display:none CSS技巧。
  • Will PHPMailer save me the step of having to write an email_validator() function? PHPMailer会省去我必须编写email_validator()函数的步骤吗?
  • Will PHPMailer save me any other time of having to write any custom functions? PHPMailer是否会在任何其他时间保存我不得不编写任何自定义函数?

Thanks! 谢谢! With any luck, I'll be answering questions soon. 运气好的话,我很快就会回答问题。 Lol 大声笑

Here is all I could think of in one sitting, forgive me if there are any glaring omissions. 这就是我能想到的一切,如果有任何明显的遗漏,请原谅我。

Advantages to using PHP's built-in mail function, no external library/wrapper: 使用PHP的内置邮件功能的优点,没有外部库/包装器:

  • You don't need anything outside of PHP. 你不需要PHP之外的任何东西。
  • You don't need to learn a new API. 您无需学习新的API。
  • You don't have to worry about a PHP upgrade or such breaking the script. 您不必担心PHP升级或破坏脚本。
  • You don't have to worry about an updated version not working on your PHP installation. 您不必担心不能在PHP安装上运行的更新版本。
  • You don't have to worry about potential security vulnerabilities as a result of using that script. 您不必担心使用该脚本可能导致的安全漏洞。
  • If it's a simple task, you'll be done in a few minutes. 如果这是一项简单的任务,您将在几分钟内完成。

Advantages to using an external library/wrapper: 使用外部库/包装器的优点:

  • If you need to introduce more complexity into your emailing, you can do so quite easily. 如果您需要在电子邮件中引入更多复杂性,则可以非常轻松地完成。 Adding attachments, inline images and such are not much fun using PHP plain mail function. 使用PHP普通邮件功能添加附件,内联图像等并不是很有趣。 External libraries (at least the good ones) have a more OOPish API. 外部库(至少是好的库)有更多的OOPish API。 Adding an attachment can be as easy as $message->addAttachment($file); 添加附件可以像$message->addAttachment($file);一样简单$message->addAttachment($file); without having to play around with headers, etc. 无需使用标题等。
  • External libraries better hide the ugly complexities of tasks such as adding attachments, character encodings and inline images. 外部库可以更好地隐藏任务的丑陋复杂性,例如添加附件,字符编码和内嵌图像。
  • Using a library now will save you the hassle of having to learn it in the future when you do need the additional complexity/functionality. 现在用的是库将节省您不必了解它在将来,当需要额外的复杂性/功能的麻烦。
  • External libraries probably (I'm really not sure which ones, and to what extent) address certain vulnerabilities that PHP's mail does not. 外部库可能 (我真的不确定哪些,以及在多大程度上)解决PHP的邮件没有的某些漏洞。

If I can think of anything else, I'll be sure to add it. 如果我能想到其他任何事情,我一定会加上它。

This will maybe not really answer all your questions, but it won't hurt either, I guess... 这可能不会真正回答你所有的问题,但它也不会伤害,我猜...

Whatever you want to do, I would not go with mail() : sending a mail is not such an easy task, and using an existing library/framework will always be a good idea : it will solve many problems you probably have not even thought about -- even if you don't need to send lots of mails. 无论你想做什么, 我都不会使用mail() :发送邮件并不是一件容易的事,使用现有的库/框架总是一个好主意:它会解决许多你甚至没想过的问题关于 - 即使你不需要发送大量邮件。


About your specific questions, maybe other answers will say something else and/or get your more informations, but any "good" library created to send mails should deal with those kind of problems... Else, you should probably search for another library ^^ 关于你的具体问题,也许其他答案会说些别的和/或获得你的更多信息,但是为发送邮件而创建的任何“好”库都应该处理这些问题......否则,你应该搜索另一个库^ ^

Still, testing a couple of dumb non-addresses will allow you to be 100% sure ;-) 尽管如此,测试一些愚蠢的非地址将让你100%肯定;-)


Another solution to be quite sure is to check the source of the library ;-) 另一个非常明确的解决方案是检查库的来源;-)

In the source of version 2.2.1, you'll find stuff like this : 在2.2.1版的源代码中,你会发现这样的东西:

class.phpmailer.php , function AddAnAddress , line 413, you'll see this : class.phpmailer.php ,函数AddAnAddress ,第413行,你会看到:

if (!self::ValidateAddress($address)) {
  $this->SetError($this->Lang('invalid_address').': '. $address);
  if ($this->exceptions) {
    throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  }
  echo $this->Lang('invalid_address').': '.$address;
  return false;
}

And it seems this function is used by the other functions that add an address... So, I suppose there's some kind of email-addresses validation ;-) 并且似乎这个函数被添加地址的其他函数使用......所以,我想有某种电子邮件地址验证;-)
That'll answer at least one of your questions ^^ 这至少会回答你的一个问题^^


PHPMailer is not the only solution that exists, btw ; PHPMailer不是唯一存在的解决方案,顺便说一下; there are plenty of others, like, for instance : 还有很多其他的,例如:

As Pascal MARTIN mentioned, sending an email isn't as straight forward and easy as some people just assume it is. 正如Pascal MARTIN所提到的,发送电子邮件并不像有些人认为的那样简单直接。 To answer your questions directly. 直接回答你的问题。 Yes PHPMailer does do some validation, but it's not super-advanced, but should be enough for your uses. 是的PHPMailer确实做了一些验证,但它不是超级先进的,但应该足以满足您的需求。 And PHPMailer will save you some time depending on what custom functions you will need. PHPMailer将为您节省一些时间,具体取决于您需要的自定义功能。 Some things to consider though: 有些事情需要考虑:

  • HTML vs plain text. HTML与纯文本。 If the emails are only ever going to you, this probably isn't as big of a deal. 如果电子邮件只发给你,这可能不是什么大不了的事。 But if you're ever sending emails to your users (say a confirmation email) you want to be able to support both HTML and plain text clients. 但是,如果您要向用户发送电子邮件(例如确认电子邮件),您希望能够同时支持HTML和纯文本客户端。 PHPMailer (and Zend_Mail) make this very easy to do. PHPMailer(和Zend_Mail)使这很容易做到。
  • SMTP. SMTP。 This is another one that is really important if you're sending email to your users, but not so much if it's just an email to your self. 如果您向用户发送电子邮件,这是另一个非常重要的,但如果它只是一封给您自己的电子邮件,那么这一点非常重要。 Using php's regular mail() function the email will be sent via sendmail, which almost all *nix installs come with out of the box (especially servers). 使用php的常规mail()函数,电子邮件将通过sendmail发送,几乎所有* nix安装都是开箱即用的(特别是服务器)。 As a result, spam filters aren't very friendly towards it. 因此,垃圾邮件过滤器对它不是很友好。 If you have a regular SMTP server setup with a trusted MX record (or if you have a gmail account) you can send through that using SMTP, which will help reduce the chances of your mail being flagged as spam. 如果您有一个带有可信MX记录的常规SMTP服务器设置(或者如果您有一个Gmail帐户),您可以使用SMTP发送,这将有助于减少您的邮件被标记为垃圾邮件的可能性。

In addition to just PHPMailer Zend_Mail is a good one to check out to (it's part of the Zend Framework ). 除了PHPMailer之外,Zend_Mail是一个很好的检查(它是Zend Framework的一部分)。 However that may be a bit much for a simple contact form. 然而,对于简单的联系表格,这可能有点多。

PHPMailer是我的选择,因为它允许我发送SMTP电子邮件到谷歌而不安装任何库或配置邮件服务器,这样我就不必担心垃圾邮件相关的问题。

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

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