7 回答
TA贡献1821条经验 获得超4个赞
在邮件功能中添加邮件标题
$header = "From: noreply@example.com\r\n"; $header.= "MIME-Version: 1.0\r\n"; $header.= "Content-Type: text/html; charset=ISO-8859-1\r\n"; $header.= "X-Priority: 1\r\n"; $status = mail($to, $subject, $message, $header);if($status){ echo '<p>Your mail has been sent!</p>';} else { echo '<p>Something went wrong, Please try again!</p>'; }
TA贡献1770条经验 获得超3个赞
始终尝试在邮件功能中发送标题。
如果您通过localhost发送邮件,请执行smtp设置以发送邮件。
如果您通过服务器发送邮件,请检查服务器上是否启用了电子邮件发送功能。
TA贡献1824条经验 获得超8个赞
通过执行以下操作,它在000webhost上为我工作:
$headers = "MIME-Version: 1.0" . "\r\n";$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";$headers .= "From: ". $from. "\r\n";$headers .= "Reply-To: ". $from. "\r\n";$headers .= "X-Mailer: PHP/" . phpversion();$headers .= "X-Priority: 1" . "\r\n";
发送电子邮件时直接输入电子邮件地址
mail('email@gmail.com', $subject, $message, $headers)
使用''与否""
此代码有效,但收到的电子邮件滞后半小时
TA贡献1796条经验 获得超10个赞
大部分mail()功能在共享主机中被禁用。更好的选择是使用SMTP。最好的选择是Gmail或SendGrid。
SMTPconfig.php
<?php
$SmtpServer="smtp.*.*";
$SmtpPort="2525"; //default
$SmtpUser="***";
$SmtpPass="***";
?>
SMTPmail.php
<?php
class SMTPClient
{
function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{
$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
if ($SmtpPort == "")
{
$this->PortSMTP = 25;
}
else
{
$this->PortSMTP = $SmtpPort;
}
}
function SendMail ()
{
$newLine = "\r\n";
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
{
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");
$talk["hello"] = fgets ( $SMTPIN, 1024 );
fputs($SMTPIN, "auth login\r\n");
$talk["res"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpUser."\r\n");
$talk["user"]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpPass."\r\n");
$talk["pass"]=fgets($SMTPIN,256);
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");
$talk["From"] = fgets ( $SMTPIN, 1024 );
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");
$talk["To"] = fgets ($SMTPIN, 1024);
fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\n".$headers."\n\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ...
fputs ($SMTPIN, "QUIT\r\n");
fclose($SMTPIN);
//
}
return $talk;
}
}
?>
contact_email.php
<?php
include('SMTPconfig.php');
include('SMTPmail.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$to = "";
$from = $_POST['email'];
$subject = "Enquiry";
$body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
}
?>
- 7 回答
- 0 关注
- 4029 浏览
添加回答
举报