我是namecheap域服务器的新手.我试图在该namecheap服务器上发送一个简单的邮件.它没有发送邮件并返回空值而不是任何错误.
这是我的示例代码.
$to = "raamanmca@gmail.com";
$subject = "HTML email";
$message = "Hello this is testing mail";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <ramalingam@binaryswan.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Mail sent...";
}
else
{
echo "Mail not sent";
}
Run Code Online (Sandbox Code Playgroud)
假设我在namecheap服务器邮件中分配$ to和$ from mailID,然后成功发送邮件.
例:
$to='test@binaryswan.com'
$from='hello@binaryswan.com'
Run Code Online (Sandbox Code Playgroud)
但我正在将邮件$更改为OR $进入gmail服务器,就像test@gmail.com那时它不会发送邮件一样,也会返回空值而不会出错.怎么修.
从(不是从PHP邮件()方法接收电子邮件)只有在我们的服务器上托管域可以用在"发件人"字段.任何未托管我们的域都无法添加到"发件人"字段中.我们必须采取此措施来防止使用论坛,留言簿和联系表单脚本发送垃圾邮件.为使您的网站脚本正常工作,您应将"发件人"字段设置为已在cPanel中创建的电子邮件帐户.
它与我的问题,但我不知道如何"设置'发件人’字段在我的cPanel电子邮件帐户."
达伦是对的。我正在将 PHP mail() 函数更改为 PHPMailer mail() 方法。下载链接 -https://github.com/PHPMailer/PHPMailer
现在邮件已成功发送。感谢评论。这是答案代码:
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->Host = 'smtp1.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('ramalingam.p@pickzy.com', 'Rama Lingam'); // Add a recipient
$mail->addAddress('raamanmca@gmail.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->isHTML(true);
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo '<br>Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3733 次 |
| 最近记录: |