使用 PHP Mailer 发送 PHP 电子邮件

标签 php html email

我正在创建一个表单,用于将电子邮件发送到输入其中的电子邮件。我已经在我的服务器上安装了 PHP 邮件程序。

目前,当通过 PHP 发送电子邮件时,他们发送的是这样的: Example of look.

当我希望它们看起来像这样时,所有发送的电子邮件都是通过托管服务器发送的。

Example of expected look

就像在域上发送的任何其他电子邮件一样,它们应该由 domain.com 而不是服务器发送。

我只是对此进行测试,因此使用一个简单的表单作为概念证明。

<form method="post" name="process.php" action="process.php">
<p>Name:</p><br><input type="text" name="name"><br><br>
<p>Email Address:</p><br><input type="text" name="email"><br><br>
<br>
<input type="submit" value="Send Email">
</form>

然后我使用这个 PHP 发送电子邮件:

<?php
$name = $_POST['name'];
$email = $_POST['email'];

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply@domain.com';
$mail->Password = 'password';

$email_from = "noreply@domain.com";
$email_subject = "Test Email";

$to = $email;
$headers = "From: noreply@domain.com \r\n";
$headers .= "Bcc: noreply@domain.com \r\n";
$headers .= "Reply-To: me@domain.com \r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";


$email_body = <<<EOM
<p color="#000000">Hello, $name.<br><br> This is a test email for mailing from the domain rather than the server.<br><br> </p>
EOM
    ;

mail($to, $email_subject, $email_body, $headers);

?>

基本上,我希望通过我的域邮寄 PHP 电子邮件,但我不知道如何执行此操作,因此将不胜感激,我的虚拟主机似乎无法帮助我解决此问题。

提前致谢。

更新

此表单代码。

<h1>The email gets sent to a bookings address.</h1>

<form method="post" name="process.php" action="process.php">
<p class= "whitetextsubsmall">Name:</p><br><input type="text" name="name"><br><br>
<p class= "whitetextsubsmall">Email Address:</p><br><input type="text" name="email"><br><br>
<br>
<input type="submit" value="Send Email">
</form>

process.php

的代码
<?php
$name = $_POST['name'];
$email = $_POST['email'];

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply@domain.com'; /* This is the sender of the bookings. */
$mail->Password = 'password';


$mail->setFrom('noreply@domain.com');
$mail->addAddress('bookings@domain.com', 'Company Bookings');
$mail->addReplyTo($email, $name); /* Reply to the user who submitted the form from the bookings email. */
$mail->Subject = 'Booking Request Test';

$mail->isHTML(TRUE);
$mail->Body = 'Message test <br> Booking Request from: $name <br><br> Email: $email.';

if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

最佳答案

您正在混合使用 PHPMailer 语法和 PHP mail() 语法。

对于 PHPMailer,请在您的代码中使用以下内容。

<?php
$name = $_POST['name'];
$email = $_POST['email'];

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply@domain.com';
$mail->Password = 'password';

/* Set the mail sender. */
   $mail->setFrom($email, $name);

/* Add a recipient. */
   $mail->addAddress('noreply@domain.com', 'earningtoanimate');

/* Add a replyto. */
$mail->addReplyTo($email, $name);

/* Add a CC and Bcc. */
$mail->addCC('noreply2@domain.com', 'earningtoanimate2');
$mail->addBCC('noreply3@domain.com', 'earningtoanimate3');

/* Add email subject. */
$mail->Subject = 'Test Email';

/* Add email body. */
$mail->isHTML(TRUE);
$mail->Body = 'There goes your message.';

/* Finally send the mail. */
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}

测试以上内容并提供您的反馈。注意,我没有测试代码。只是将它们写在这里,以便在需要时进行一些编辑。

如需进一步阅读,请访问 PHPMailer documentation

关于使用 PHP Mailer 发送 PHP 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780542/

相关文章:

php - 激活/停用选择框选项的 spry 验证

c++ - 如何在 C++ 中读取 Evolution 邮件管道?

.net - 处理收到的电子邮件

c# - Blazor 客户端发送电子邮件

php - 从字符串中选择 50 个单词

php - 使用 Jenkins/Hudson 部署/持续集成 Symfony 2 应用程序

php - 优化自定义 WordPress SQL 查询

javascript - 移动一个 div 元素

html - 背景图像高度和宽度在 IE11 上不起作用

javascript - jQuery onclick 错误函数