如何配置 PHP 发送电子邮件?

How to configure PHP to send e-mail?(如何配置 PHP 发送电子邮件?)
本文介绍了如何配置 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();

这篇关于如何配置 PHP 发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)