我在从表单发送电子邮件时遇到问题,所以我在网站上寻找一些答案,我看到有人发帖:https://github.com/PHPMailer/PHPMailer
但我没有意识到如何使用它?我应该把什么放在我的网站文件夹中?我该怎么办?再次使用mail
标记?
我该怎么做,例如只是一个普通的表格:
姓名:,电子邮件:,内容:,提交
然后它会发送到我的电子邮箱。
答案 0 :(得分:1)
很简单,首先扩展PHPMailer并设置网站的默认值:
require("class.phpmailer.php");
class my_phpmailer extends phpmailer {
// Set default variables for all new objects
var $From = "from@example.com";
var $FromName = "Mailer";
var $Host = "smtp1.example.com;smtp2.example.com";
var $Mailer = "smtp"; // Alternative to IsSMTP()
var $WordWrap = 75;
// Replace the default error_handler
function error_handler($msg) {
print("My Site Error");
print("Description:");
printf("%s", $msg);
exit;
}
// Create an additional function
function do_something($something) {
// Place your new code here
}
}
然后在需要的地方包含上述脚本(在此示例中,它名为mail.inc.php
),并在您网站的某处使用新创建的my_phpmailer
类,例如 validate.php :
<?php
if(isset($_POST["name"]))
{
require("mail.inc.php");//or the name of the first script
// Instantiate your new class
$mail = new my_phpmailer;
// Now you only need to add the necessary stuff
$mail->AddAddress($_POST["email"], $_POST["name"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
if(!$mail->Send())
{
echo "There was an error sending the message";
exit;
}
echo "Message was sent successfully";
}
else{
echo "No post values found!";
}
?>
以下是一个示例表单:
<form name="sendmail" action="validate.php" method="post">
<input name="name" type="text" required>
<input name="email" type="email" required>
<input name="subject" type="text" required>
<textarea name="message" required></textarea>
<button type="submit">Send Mail</button>
</form>