如何配置 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 服务器吗?如何使用 localhost 发送邮件?请告诉我在 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();
相关文章