How to send mail using PHP?

2019-01-08 01:25发布

I'm using Windows Vista OS. PHP, MySQL as the database and Apache web server.

I want to send notification to those who want to join in my site. But the problem is when I click submit. It doesn't send anything to the email address of the user.

What to do you think is the best solution for this?

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }
?>

标签: php email
7条回答
相关推荐>>
2楼-- · 2019-01-08 01:58

There are many ways to send email via PHP. Using the internal mail() function is the first if you are not using any framework. I suggest using the Zend_Mail component of the Zend Framework. I've worked with it and it works very well and grants you a nice Object Oriented way to send emails using PHP. But if you have your reasons to use the mail() function, then I think you might need to know these: PHP mail() does not send email itself. It utilizes other tools to send emails. If you are running your application in Unix systems, mail() tries to send the email by using the sendmail program which is commonly installed on most Unix-like systems. On Windows platforms, there is no sendmail installed so PHP will try to send the email using an SMTP server. so you should tell PHP where this SMTP server is and provide it with the username/password of the SMTP server, if there are any. You can do this by editing your PHP configuration file, php.ini. This file is a text-based configuration file that PHP will use when executed. Find your php.ini file and modify these values in this file:

SMTP = localhost
smtp_port = 25
sendmail_from = me@example.com

If you do not know where is your php.ini file, create a simple PHP file, like info.php and put this code in it:

<?php
phpinfo();
?>

Run your page and search for .ini. Now you know where your php.ini file is.

查看更多
走好不送
3楼-- · 2019-01-08 02:06

It looks that you dont have a mailserver installed. Apache or PHP are not sending mails for you. Php is registering functions for that but internally you have to configure a mail smtp server to do the actual sending.

Check this post.

查看更多
男人必须洒脱
4楼-- · 2019-01-08 02:07

Just check out "How to Send Email from a PHP Script". It uses the mail function to send mail and it further gives configuration for local and remote SMTP configuration too.

查看更多
看我几分像从前
5楼-- · 2019-01-08 02:09

You have several options:

  • there's built in mail function, but it requires that some MTA is running on the machine, doesn't support authentication, and you'll need to work hard to send attachments
  • there's Pear Mail package, if you like to use Pear stuff
  • there's a good PHPMailer class which I used in past few years and it works really great. It like it because it is very simple and easy to include in your project (just a simple include call), yet powerful
查看更多
何必那么认真
6楼-- · 2019-01-08 02:12

Zend_Mail of the Zend Framework does a very neat job with sending E-Mails!! You don't need to use the whole of Zend Framework, you can just use Zend_Mail!

查看更多
Summer. ? 凉城
7楼-- · 2019-01-08 02:13

I would recommend using the swiftmailer library, the project has very good documentation and is easy to use. It offers several advantages to using the default PHP mail() function aswell.

http://swiftmailer.org/

查看更多
登录 后发表回答