带有PHP Mailer的PHP电子邮件

学习动画

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

当前,当通过PHP发送电子邮件时,它们的发送方式如下: 外观示例。

当我希望它们发送的所有电子邮件都是通过托管服务器发送的。

预期外观示例

就像在域上发送的任何其他电子邮件一样,这些电子邮件应通过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 = '[email protected]';
$mail->Password = 'password';

$email_from = "[email protected]";
$email_subject = "Test Email";

$to = $email;
$headers = "From: [email protected] \r\n";
$headers .= "Bcc: [email protected] \r\n";
$headers .= "Reply-To: [email protected] \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电子邮件,但我不知道该怎么做,因此不胜感激,我的网络托管服务商似乎无法帮助我。

提前致谢。

UPDATE

此代码为表格。

<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 = '[email protected]'; /* This is the sender of the bookings. */
$mail->Password = 'password';


$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]', '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 = '[email protected]';
$mail->Password = 'password';

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

/* Add a recipient. */
   $mail->addAddress('[email protected]', 'earningtoanimate');

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

/* Add a CC and Bcc. */
$mail->addCC('[email protected]', 'earningtoanimate2');
$mail->addBCC('[email protected]', '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文档。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP Mailer 返回 true 但未收到电子邮件

从PHP Mailer标头修剪电子邮件地址

向 CCN 用户 PHP MAILER 发送不同的电子邮件

如何阻止PHP Mailer在每个电子邮件上附加所有电子邮件地址?

PHP同时使用PHP Mailer将内容循环到电子邮件中

在PHP中制作带有HTML标签的电子邮件

PHP Mail()无法发送带有附件的电子邮件

php发送带有PDF附件的电子邮件

PHP Contactform带有确认电子邮件

发送带有附件的PHP电子邮件(失败)

通过带有IF条件的PHP发送电子邮件

PHP Mailer:如何从电子邮件中的选择选项中查看信息

PHP Mailer未显示任何错误,但不发送电子邮件

为什么从PHP-Mailer发送电子邮件很慢

PHP - 电子邮件错误

php电子邮件域名

空白的PHP电子邮件

电子邮件验证php

从php表单使用php-mailer库通过本地主机Xampp发送电子邮件的问题/错误

在我的后缀邮件队列中找到所有带有电子邮件的PHP脚本

带有多个带有文本消息的附件的PHP电子邮件

TinyMCE编辑器的格式不会使用PHP-Mailer通过电子邮件发送

通过PHP Mailer使用实时服务器SMTP服务器发送电子邮件不起作用

如何使用PHP-Mailer在电子邮件正文中分别显示电话号码和密码

PHP Mailer仅在添加了打印到xml文件的功能后才发送重复的电子邮件

使用@nestjs/mailer 发送带有 Excel 附件的电子邮件

PHP电子邮件重定向

PHP管道到脚本电子邮件

用php验证电子邮件