我正在创建一个表单,该表单将电子邮件发送到其中输入的电子邮件。我已经在服务器上安装了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] 删除。
我来说两句