如何使用带有php联系人表单的本地xampp从我的mac发送邮件
我在2016年的macbook pro上安装了xampp 7.1。我的源文件夹中有一个php联系人表单,它与我的html一起由chrome成功呈现。但是,我需要配置smtp设置,以便从mac本地测试某些功能 我在xampp etc文件夹中发现了一个php.ini,其中包含一些邮件功能。 我找到了其他引用phpmailer的文章,但由于它们已经有几年的历史了,我认为新版本的xamppformac可能具有内置的所有功能 我可以修改这个php.ini文件以使用gmail邮箱作为收件人吗?怎么会这样 提前谢谢 以下是邮件功能的复制粘贴:如何使用带有php联系人表单的本地xampp从我的mac发送邮件,php,html,xampp,smtp,phpmailer,Php,Html,Xampp,Smtp,Phpmailer,我在2016年的macbook pro上安装了xampp 7.1。我的源文件夹中有一个php联系人表单,它与我的html一起由chrome成功呈现。但是,我需要配置smtp设置,以便从mac本地测试某些功能 我在xampp etc文件夹中发现了一个php.ini,其中包含一些邮件功能。 我找到了其他引用phpmailer的文章,但由于它们已经有几年的历史了,我认为新版本的xamppformac可能具有内置的所有功能 我可以修改这个php.ini文件以使用gmail邮箱作为收件人吗?怎么会这样
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail
-t -i").
; http://php.net/sendmail-path
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra
parameters
; to the sendmail binary. These parameters will always replace the
value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script
followed by the filename
mail.add_x_header=On
; Log all mail() calls including the full path of the script, line #,
to address and headers
;mail.log =
如果你想从你的应用服务器向你自己的gmail收件箱发送邮件,你不需要修改这些设置;使用是一种更简单的方法。让我们一步一步地开始
- 允许-或-配置;归功于@Synchro李>
- 下载并将
、PHPMailer.php
和SMTP.php
解压缩到Exception.php
文件夹中xampp/htdocs
- 编写一个示例发送邮件页面
:(修改到您的真实帐户)send.php
- 当以上所有内容就绪后,在应用服务器上发出
的url以发送示例电子邮件send.php
- 基于此示例,您可以编写一个用于实际使用的联系人表单。您可能还希望更改
的位置,然后应修改示例代码中的路径,以指向它们所在的新位置李>PHPMailer
使用
PHPMailer异常
类,所以您捕获的是全局异常,而不仅仅是PHPMailer异常。谢谢您,Ken,Synchro。我马上给你打一针!我能够完成我的目标。再次感谢。最后一个问题。我知道这是我的实验室环境。一旦我发布了我的网站,比如说,go daddy,该怎么办。我应该保留这个phpmailer解决方案还是使用其他解决方案?@tobe1424:我不熟悉GoDaddy的服务。通常情况下,如果把它们放在虚拟主机上就可以了。在代码中使用相对路径;您可能还必须为该主机配置XOAUTH2。明白了。谢谢下面是我最后用于php联系人页面的代码。我刚刚将我的contacts.php表单上传到public_html dir,从用户的角度来看,它可以无缝工作——我接收电子邮件
<?php
require_once('SMTP.php');
require_once('PHPMailer.php');
require_once('Exception.php');
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;
$mail=new PHPMailer(true); // Passing `true` enables exceptions
try {
//settings
$mail->SMTPDebug=2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host='smtp.gmail.com';
$mail->SMTPAuth=true; // Enable SMTP authentication
$mail->Username='YourAccount@gmail.com'; // SMTP username
$mail->Password='YourPassword'; // SMTP password
$mail->SMTPSecure='ssl';
$mail->Port=465;
$mail->setFrom('sender@whatever.com', 'optional sender name');
//recipient
$mail->addAddress('YourAccount@gmail.com', 'optional recipient name'); // Add a recipient
//content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject='Here is the subject';
$mail->Body='This is the HTML message body <b>in bold!</b>';
$mail->AltBody='This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
}
catch(Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: '.$mail->ErrorInfo;
}
?>