通过php发送消息

时间:2016-03-27 07:51:35

标签: php

我有问题通过php从主机电子邮件发送消息到有人发送脚本的电子邮件。喜欢注册码等...

这是我的代码:

        $to = "$email";
    $from = "support@webiste.com";
    $subject = 'Web Site';
    $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>WebSite</title></head><body style="padding: 20px;"><h1>WEBSITE</h1><p>Hello '.$name.' '.$lname.'</p><h3>Click the link below to activate your account: </h3><p><a style="text-decoration: none;" href="http://www.website.com/MB/activation.php?id='.$rewr.'&re='.$re.'">Click Here</a></p></body></html>';
    $headers = "From: $from\n";
    $headers = "MIME-Version: 1.0\n";
    $headers = "Content-type: text/html; charset=iso-8859-1\n";
    if(mail($to, $subject, $message, $headers)){

echo "Sent";}else echo "failed";

我每次都收到失败的消息

1 个答案:

答案 0 :(得分:0)

sendmail依赖于服务器,更好的解决方案是使用Pear邮件。

include_once 'Mail.php';
include_once 'Mail/mime.php' ;

$recipients  = '<' . $this->to . '>'; 

$text =  $this->message['text'];
$html =  $this->message['html'];

$crlf = "\n";

$hdrs = array('From' => $this->from,'To' => $this->to,'Subject' =>         $this->subject);
$mime = new Mail_mime(array('eol' => $crlf));

if(!empty($text)){$mime->setTXTBody($text);}
if(!empty($html)){$mime->setHTMLBody($html);}

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$params["host"] = MailSMTP; 
$params["port"] =  MailPort;
$params["auth"] = true;  
$params["username"] = $username; 
$params["password"] = $password; 

$mail =& Mail::factory('smtp',$params);
$value = $mail->send($recipients,$hdrs, $body);

if(PEAR::isError($value)) { 
return false;
}

以上是我自己使用的代码。它将发送多部分邮件,即HTML和纯文本,以便他们可以在任何设备上阅读它,它使用Pear邮件并发送给1个或多个收件人。 Pear是PHP的扩展。