用于使用smtp发送邮件的PHP代码

时间:2014-04-11 12:48:50

标签: php

这是我的代码,我没有通过此代码收到任何邮件。

$email = $HTTP_POST_VARS[email];
$mailto ="nr.shubha@gmail.com"; 
$mailsubj = "Form submission"; 
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS); 
$mailbody = "Email form the web site form:\n";
echo $HTTP_POST_VARS; 
while (list ($key, $val) = each ($HTTP_POST_VARS)) { 
     $mailbody .= "$key : $val\n"; 
} 
if (!eregi("\n",$HTTP_POST_VARS[email])) { 
    mail($mailto, $mailsubj, $mailbody, $mailhead); 
}

print_r($HTTP_POST_VARS);

3 个答案:

答案 0 :(得分:1)

我强烈建议您使用预制课程,例如https://github.com/PHPMailer/PHPMailer

它比使用普通的PHP代码更“垃圾邮件安全”,并帮助您使用图像嵌入,附件等...

答案 1 :(得分:1)

我建议您使用PHPMailer Library。效果很好。

以下是我的工作样本

require_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP

//This part is for authentication
$mail->Host = "mail.example.com";  // specify main and backup server
$mail->SMTPAuth = "SMTPAuth";     // turn on SMTP authentication
$mail->Username = "user@example.com";  // SMTP username
$mail->Password = "****"; // SMTP password

//the receiver will see that the sender address is this
$mail->From = "ihavesentit@example.com";
$mail->FromName = "The Sender";

//You can use AddAdress() and AddCC() functions several times for different receivers
$mail->AddAddress("receiver@example.com");
$mail->AddCC('another_receiver@example.com');

$mail->AddReplyTo("reply-to-me@example.com");

$mail->WordWrap = 50; // set word wrap to 50 characters (arbitrary)

$mail->IsHTML(true); // set email format to HTML

$mail->Subject = "mail subject";

$mail->Body = "<p>mail content will be typed here</p>";

$mail->AltBody = "will be used if the receiver does not accept HTML content e-mails";

//final and the most important action
$mail->Send();

答案 2 :(得分:0)

这仅适用于实时服务器托管时。它不适用于localhost。

因为SMTP仅适用于不在“localhost”上的实时托管服务器。