0

如果用户想向我的网络邮箱发送电子邮件,此代码是否足够?还是我需要做出改变?

<?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";

}

?>
4

4 回答 4

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);
?>

注意:邮件功能仅适用于实时服务器而不适用于本地服务器。

于 2012-12-31T10:44:13.020 回答
1

我建议使用编码 (UTF8) 在电子邮件中添加标题,对主题行进行编码,这样您就不会出现乱码(例如,如果您使用其他非拉丁字符)并处理基本事件,无论成功与否。

<?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
}
?>
于 2012-12-31T10:46:39.970 回答
0

这对于简单的邮件来说是可以的。但mail()函数不适合循环处理大量邮件。该函数为每封邮件打开和关闭一个SMTP套接字,效率不高。要发送大量电子邮件,请参阅 PEAR::MailPEAR::Mail_Queue包。

于 2012-12-31T10:09:57.250 回答
-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";

}
于 2012-12-31T10:17:47.713 回答