我拥有我的域名 .com,它托管在不同的域名提供商中。 我想使用 PhpMailer,但我的虚拟主机不向我提供 SMTP 服务器,但它允许我创建我的域的电子邮件(例如 [电子邮件受保护]) 所以,我真的无法使用 phpmailer,因为它永远无法连接到 SMTP 服务器,我该怎么办?
我将我的域名引入了 onlydomains,我的虚拟主机是 000webhost。
<?php
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = '[email protected]';
$mail->FromName = 'Admin';
$mail->addAddress('[email protected]'); // Name is optional
mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'swag';
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
首先将 php 邮件程序文件下载到您的服务器(如果之前未下载)并定义如下。避免在代码中使用网络源。一旦应用此功能,它将正常工作,并且邮件将从您的邮件 ID 发送。
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "your mail id"; // SMTP account username
$mail->Password = "password"; // SMTP account password
//The following code allows you to send mail automatically once the code is run
$mail->SetFrom('to mail id', 'name'); // FROM
$mail->AddReplyTo('mail id', 'name'); // Reply TO
$mail->AddAddress('recipient id'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n some text.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
执行上述代码后,如果仍然无法发送邮件,请执行以下操作: 在 class.smtp.php 文件中,在“连接到 SMTP 服务器”行之前添加以下给出的两行,它将正常工作。
$host = "ssl://smtp.gmail.com";
$port = 465;