我拥有自己的域名.com,它托管在域名的不同提供商处。 我想使用PhpMailer,但我的网站主机不提供SMTP服务器,但它允许我创建我的域的电子邮件(例如sdfsdfsfd@mydomain.com) 所以,我真的不能使用phpmailer,因为它永远不会连接到SMTP服务器,我该怎么办?
我的域名只在域名中,我的虚拟主机是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 = 'noreply@mydomain.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = 'noreply@mydomain.com';
$mail->FromName = 'Admin';
$mail->addAddress('sdfsdf@hotmail.com'); // 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';
}
?>
答案 0 :(得分:0)
最初将php邮件程序文件下载到您的服务器(如果之前没有下载)&amp;定义如下。避免在你的代码中使用网络资源。一旦你申请它,它将工作正常,邮件将从你的邮件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服务器”&amp;行之前添加以下给定的两行。它会正常工作。
$host = "ssl://smtp.gmail.com";
$port = 465;