2014-01-29 205 views
0

对不起,我的英文。 我通过邮件梨包试图发送E-迈:通过梨邮件发送邮件

require_once "Mail.php"; 

$from = '<[email protected]>'; 
$to = '<[email protected]>'; 
$subject = 'Hi!'; 
$body = "Hi,\n\nHow are you?"; 

$headers = array(
    'From' => $from, 
    'To' => $to, 
    'Subject' => $subject 
); 

$smtp = Mail::factory('smtp', array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => '465', 
     'auth' => true, 
     'username' => '[email protected]', 
     'password' => 'pass' 
    )); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo('<p>' . $mail->getMessage() . '</p>'); 
} else { 
    echo('<p>Message successfully sent!</p>'); 
} 

//email addresses are changed 

但结果我有错误: 无法连接到SSL://smtp.gmail.com:465 [SMTP:无法连接套接字:权限被拒绝(代码:-1,响应:)] Openssl已启用。 谢谢。

回答

0

在您可能要发送的HTML电子邮件的某个阶段,所以你还需要使用由PEAR提供的邮件/ MIME包。我也包括这一点。

<?php 
require_once "Mail.php"; 
require_once "Mail/mime.php"; 

$from = "<[email protected]>"; 
$to = "[email protected]"; // the email address 
$host = "smtp.gmail.com"; 
$port = "587"; 
// use the first example username if sending from gmail, as full email address 
// is required 
$username = "[email protected]"; 
$username = "fred"; 
$password = "secure"; 
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject); 
$mailbody = "<html><body>...</body></html>"; 

$mime = new Mail_mime(); 
$mime->setHTMLBody($mailbody); 
$body = $mime->get(); 
$headers = $mime->headers($headers); 
$smtp = Mail::factory(
    'smtp',array (
     'host' => $host, 
     'auth' => true, 
     'username' => $username, 
     'password' => $password, 
     'port' => $port 
    ) 
); 

// send email 
$mail = $smtp->send($to, $headers, $body); 
if (PEAR::isError($mail)) { 
    echo($mail->getMessage()); 
} else { 
    echo "<b><Center>Succesfully sent email to</b>$to</center>"; 
}