如何配置PHP发送电子邮件? [英] How to configure PHP to send e-mail?
本文介绍了如何配置PHP发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用php脚本向我的网站的用户发送邮件。我试过在php中使用邮件功能。
我的代码如下:
I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:
$to = "myweb@gmail.com";
$subject = "Test mail";
$message = "My message";
$from = "webp@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
当我尝试运行程序这是我得到的:
When I try running the program this is what I get:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().
请告诉我要包含在$ from变量中的地址。我需要一个smtp服务器吗?如何使用本地主机发送邮件?请告诉我在php.ini文件中究竟要修改的内容
Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file
我是新来的..请帮助我..
I am new to all this.. Please help me..
推荐答案
改用PHPMailer: https://github.com / PHPMailer / PHPMailer
Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer
如何使用它:
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender@gmail.com';
$mail->Password = '123!@#';
$mail->SetFrom('me.sender@gmail.com', $name);
$mail->AddReplyTo('no-reply@mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1@gmail.com', 'title1');
$mail->AddAddress('abc2@gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();
这篇关于如何配置PHP发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文