php - mail() 错误地向 cPanel 默认帐户发送电子邮件
问题描述
我从 GoDaddy 购买了一个域并将其链接到 Office 365(通过 MX 记录)。这意味着我在 Outlook 中有几个电子邮件帐户是 @mydomain.com。
示例 Outlook 帐户:
- sales@mydomain.com
- 联系人@mydomain.com
- matt@mydomain.com
我可以通过这些帐户发送/接收电子邮件。
我的网站托管在安装了 cPanel 的基本虚拟主机上,这意味着我获得了一个“默认”电子邮件帐户。例如:default@mydomain.com
。我在我的网站(联系表格)上编写了一个 PHP 脚本,通过mail()
to发送电子邮件contact@mydomain.com
。
但是,所有电子邮件都发送到默认 cPanel 帐户default@mydomain.com
而不是 Outlook 帐户contact@mydomain.com
。
为了测试,我尝试将电子邮件发送到我的个人帐户,该帐户未托管在 mydomain 上,并且它按预期工作。电子邮件会立即发送。
为什么我的网站会错误地将电子邮件发送到 Outlook 帐户?谢谢你的时间。
编辑:
请求脚本:
<?php
$uploadedFile = $statusMsg = '';
if (isset($_POST['submit']))
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if(!empty($first_name) && !empty($last_name) && !empty($message))
{
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
$uploadStatus = 1;
if(!empty($_FILES["attach"]["name"]))
{
$targetDir = "uploads/";
$fileName = basename($_FILES["attach"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["attach"]["tmp_name"], $targetFilePath))
{
$uploadedFile = $targetFilePath;
}
else
{
$uploadStatus = 0;
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
if($uploadStatus == 1)
{
$name = $first_name.' '.$last_name;
$mailTo = "contact@mydomain.com";//changed to my real outlook account
$htmlContent = '<h2>Contact Request Submitted</h2>
<p><b>Name:</b> '.$name.'</p>
<p><b>Email:</b> '.$email.'</p>
<p><b>Phone:</b> '.$phone.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
// Header for sender info
$headers = "From: $name"." <".$email.">";
if(!empty($uploadedFile) && file_exists($uploadedFile))
{
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(is_file($uploadedFile)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($uploadedFile,"rb");
$data = @fread($fp,filesize($uploadedFile));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" .
"Content-Description: ".basename($uploadedFile)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $email;
// Send email
$mail = mail($mailTo, "Contact Form Submission from ".$name, $message, $headers, $returnpath);
// Delete attachment file from the server
@unlink($uploadedFile);
}
else
{
// Set content-type header for sending HTML email
$headers .= "\r\n". "MIME-Version: 1.0";
$headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
// Send email
$mail = mail($mailTo, 'Contact Form Submission from '.$name, $htmlContent, $headers);
}
// If mail sent
if($mail)
{
$statusMsg = "Your message has been sent. Thanks!";
}
else
{
$statusMsg = 'Your contact request submission failed, please try again.';
}
}
}
else
{
$statusMsg = 'Please enter a valid email address.';
}
}
else
{
$statusMsg = "Please fill out the required information.";
}
}
?>
解决方案
推荐阅读
- python - 通过单击 MDList 的项目切换到其他屏幕
- java - Java while 循环不等待用户输入
- python - 如何删除涉及多个数据帧过滤的迭代?
- reactjs - 将新 pdf 添加到共享 PDF 文件时 React 项目出错
- kotlin - 从反向删除给定位置的链接列表
- couchbase - Couchbase n1ql - 使用索引的 NEST 外部文档
- r - 如何在开始时间和结束时间之间按小时计算项目
- swiftui - 如何在没有实际视图的情况下仅绘制圆形阴影
- javascript - API Getaway 上的删除函数
- sql-server - 无法从同一台机器上的 IIS(Windows Server 2008 R2)上的 Web api 访问 SQL Server 2008 R2