[英]PHPMailer not sending email with custom body
i need to send an email with PHPMailer, when trying to get the form elements, fails to send it, but if I use the sample code, it works perfectly. 在尝试获取表单元素时,我需要使用PHPMailer发送电子邮件,但无法发送,但是如果我使用示例代码,则可以正常工作。
Also, php sends no error, so the code is executed correctly, I don't understand why this happens. 另外,php不会发送错误,因此代码可以正确执行,我不明白为什么会这样。
Here is my php code: 这是我的PHP代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
//Obtener datos
try {
$name = $_POST['name'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$speciality = $_POST['speciality'];
$title = $_POST['title'];
$phone = $_POST['phone'];
$adress = $_POST['adress'];
$phone = $_POST['phone'];
$cellphone = $_POST['cellphone'];
$state = $_POST['state'];
$colony = $_POST['colony'];
$rfc = $_POST['rfc'];
//Luego tenemos que iniciar la validación por SMTP:
// $mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "krishna.hosting-mexico.net";
$mail->Username = "registros@aliar.svri.org.mx"; .
$mail->Password = "xxxx";
$mail->Port = 465;
$mail->From = "registros@aliar.svri.org.mx";
$mail->FromName = "Registros Aliar";
$mail->AddAddress("mauricioandres288@gmail.com");
//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();
$mail->IsHTML(true); // El correo se envía como HTML
$mail->Subject = "Nuevo registro de" . $name . " " . $lastName; // Este es el titulo del email.
$mail->Body = "Nuevo registro de" . $name . " " . $lastName;
$mail->Body .= "<br><h4>Datos personales: </h4>";
$mail->Body .= "<ul><li>" . $name . "</li>";
$mail->Body .= "<li>" . $lastName . "</li>";
$mail->Body .= "<li>" . $email . "</li>";
$mail->Body .= "<li>" . $speciality . "</li>";
$mail->Body .= "<li>" . $title . "</li>";
$mail->Body .= "<li>" . $phone . "</li>";
$mail->Body .= "<li>" . $adress . "</li>";
$mail->Body .= "<li>" . $phone . "</li>";
$mail->Body .= "<li>" . $cellphone . "</li>";
$mail->Body .= "<li>" . $state . "</li>";
$mail->Body .= "<li>" . $colony . "</li>";
$mail->Body .= "<li>" . $rfc . "</li></ul>";
// $mail->Body = $body;
$mail->send();
}
catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Also, I have to mention that my form is in a different php file. 另外,我不得不提到我的表单在另一个php文件中。
Change your code to this and see if it works. 将您的代码更改为此,然后查看是否可行。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
//Obtener datos
try {
$name = $_POST['name'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$speciality = $_POST['speciality'];
$title = $_POST['title'];
$phone = $_POST['phone'];
$adress = $_POST['adress'];
$phone = $_POST['phone'];
$cellphone = $_POST['cellphone'];
$state = $_POST['state'];
$colony = $_POST['colony'];
$rfc = $_POST['rfc'];
//Luego tenemos que iniciar la validación por SMTP:
// $mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "krishna.hosting-mexico.net";
$mail->Username = "registros@aliar.svri.org.mx"; .
$mail->Password = "xxxx";
$mail->Port = 465;
$mail->From = "registros@aliar.svri.org.mx";
$mail->FromName = "Registros Aliar";
$mail->AddAddress("mauricioandres288@gmail.com");
//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();
$mail->IsHTML(true); // El correo se envía como HTML
$mail->Subject = "Nuevo registro de" . $name . " " . $lastName; // Este es el titulo del email.
$body="";
$body .= "Nuevo registro de" . $name . " " . $lastName;
$body .= "<br><h4>Datos personales: </h4>";
$body .= "<ul><li>" . $name . "</li>";
$body .= "<li>" . $lastName . "</li>";
$body .= "<li>" . $email . "</li>";
$body .= "<li>" . $speciality . "</li>";
$body .= "<li>" . $title . "</li>";
$body .= "<li>" . $phone . "</li>";
$body .= "<li>" . $adress . "</li>";
$body .= "<li>" . $phone . "</li>";
$body .= "<li>" . $cellphone . "</li>";
$$body .= "<li>" . $state . "</li>";
$body .= "<li>" . $colony . "</li>";
$body .= "<li>" . $rfc . "</li></ul>";
$mail->Body = $body;
$mail->send();
}
catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.