如果用户想要向我的网络邮件发送电子邮件,这段代码是否足够?或者我需要做出改变?
<?php
$mail = $_POST['mail'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$text = $_POST['text'];
$to = "youremail@domain.com";
$message =" You received a mail from ".$name;
$message .=" Text of the message : ".$text;
if(mail($to, $subject,$message)){
echo "Your message was sent successfully.";
}
else{
echo "there's some errors to send the mail, verify your server options";
}
?>
答案 0 :(得分:1)
此代码肯定适合您。
<?php
$to = 'xyz@xyz.com';
$subject = "Your Subject";
$message ="<html><body>
<div>Here Write Your Message</div>
</body></html>";
$header='';
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From: abc@abc.com'. "\r\n";
mail($to,$subject,$message,$header);
?>
注意:邮件功能仅适用于实时服务器不适用于本地服务器。
答案 1 :(得分:1)
我建议使用编码(UTF8)向电子邮件添加标题,对主题行进行编码,这样您就不会获得Gibberish(例如,如果您使用其他非拉丁字符)并处理基本事件,成功与否。< / p>
<?php
$name = $_POST['name'];
$text = $_POST['text'];
$from = $_POST['mail'];
$to = "youremail@domain.com";
$subject = "=?utf-8?B?".base64_encode($_POST['subject'])."?=";
$message = " You received a mail from ".$name;
$message .= " Text of the message : ".$text;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "To: <$to>\r\n";
$headers .= "From: $name <$from>\r\n";
if (mail($to,$subject,$message,$headers)) {
// Do something if the email is sent
} else {
// Do something if there's an error
}
?>
答案 2 :(得分:0)
这对简单邮件来说没问题。但是mail()函数不适合循环中的大量电子邮件。该函数为每封电子邮件打开和关闭一个SMTP套接字,效率不高。要发送大量电子邮件,请参阅PEAR::Mail和PEAR::Mail_Queue个包。
答案 3 :(得分:-1)
如果您想通过在标题中设置类型的内容类型来发送电子邮件,并且邮件的接收方必须知道邮件的发件人,则此代码是不够的。代码如下:
$to = "youremail@domain.com";
$message =" You received a mail from ".$name;
$message .=" Text of the message : ".$text;
$headers = "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers = "From: ". Please enter the name of sender . "\r\n";
if(mail($to, $subject,$message,$headers)){
echo "Your message was sent successfully.";
}
else{
echo "there's some errors to send the mail, verify your server options";
}