Use PHP mail to send via smtp

喜你入骨 提交于 2019-12-01 20:59:42

According to this manual page, it is possible on Windows only.

Check these links:

Example:

Update: You can use this then, but it opens and closes the SMTP socket on each mail() function called.

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

No, the reason is that all Linux/Unix systems should have a "sendmail" tool. The benefit there is that this external tool can handle timeouts or unresponsive SMTP servers so it becomes more likely the mail is being really sent. The SMTP client implementation for Windows is a work-around for the fact that "sendmail" doesn't exist there.

My approach would be to use a sendmail-compatible tool that just talks to another server using SMTP. A simple tool for that is ssmtp (sources avialable here)

Just configure your local sendmail to use your upstream mail server as a relay! That way, you don't have to change anything on the PHP side.

It would not be a good idea to send mail directly from PHP with SMTP, because you would lose everything from error handling to queueing this way!

You can send via SMTP directly using the PEAR Mail package. You'll also need Net_SMTP installed for SMTP mail to work. On many servers, these are installed by default. You can also download a copy of these libraries locally and upload them to your site's directory or include path. That's not as ideal a solution, but it's functional.

If you're looking for a drop-in replacement for your old mail() function but which sends through SMTP instead of the PHP default, you'll need to write a translator function which sets all the parameters in the right order and such. There's an example here of just such a script -- obviously you'll have to modify it to match the settings you want: http://tltech.com/info/php-mail-via-smtp/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!