2010-12-26 12 views
3

どうやってGoogleと一緒に洋梨メールを使うことができますか?私はできた、これはあなたがGoogleと梨メールを使用しますが、MIMEメールではないが見つかりました:私は得続けるhttp://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/pear mail mimeの使い方

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

$from = "Sender <*******@googlemail.com>"; 
$to = "Receiver <*******@googlemail.com>"; 
$subject = "Welcome to SITENAME!"; 
$crlf = "\n"; 
$html = "<h1> This is HTML </h1>"; 

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


$host = "smtp.gmail.com"; 
$port = 465; 
$username = "********@googlemail.com"; 
$password = "********"; 

$mime = new Mail_mime($crlf); 
$mime->setHTMLBody($html); 

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

$smtp = Mail::factory("smtp",array("host" => $host, 
         "port" => $port, 
         "auth" => true, 
         "username" => $username, 
         "password" => $password)); 

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

if (PEAR::isError($mail)) { 
echo $mail->getMessage(); 
} else { 
echo "Message sent successfully!"; 
} 
echo "\n"; 

受信者の追加に失敗しました: [SMTP @localhost:無効な応答コードが受信 サーバから(コード:555、応答: 5.5.2構文エラーf52sm5542930wes.35)]

編集:

は電子メールが今受けている、しかし、それは次のように判明:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using= 
the PEAR Mail package and SMTP through Gmail. Enjoy! 
+0

あなたは何を意味しているのですか? –

+0

@Pekka:@johnは、HTMLの電子メールを送信したいと考えていると思います。 – stealthyninja

+0

申し訳ありませんが、PHPコードが表示されませんでした。はい、HTMLメールを送信します。^ – john

答えて

2

@ジョン:あなたが投稿のリンクからコードを使用するには、そのようにそれを修正 -

<?php 
require_once('Mail.php'); 
require_once('Mail/mime.php'); 

$from = 'Sender <[email protected]>'; 
$to = 'Receiver <[email protected]>'; 
$subject = 'Sent from PHP on my machine'; 

$text = 'This is a message I sent from <a href="http://www.php.net/">PHP</a> ' 
     . 'using the PEAR Mail package and SMTP through Gmail. Enjoy!'; 

$message = new Mail_mime(); 
$message->setTXTBody(strip_tags($text)); // for plain-text 
$message->setHTMLBody($text); 
$body = $message->get(); 

$host = 'smtp.gmail.com'; 
$port = 587; //According to Google you need to use 465 or 587 
$username = 'sender'; 
$password = 'your_password'; 

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

$smtp = Mail::factory('smtp', 
    array(
     'host' => $host, 
     'port' => $port, 
     'auth' => true, 
     'username' => $username, 
     'password' => $password)); 

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

if (PEAR::isError($mail)) { 
    echo $mail->getMessage(); 
} else { 
    echo "Message sent successfully!"; 
} 

echo "\n"; 

?> 

更新:

編集:

電子メールが今受信され

は、しかし、それは次のように判明:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using= 
the PEAR Mail package and SMTP through Gmail. Enjoy! 

@ジョン:更新

$body = $mime->get(); 

$body = $mime->get(array('text_charset' => 'utf-8')); 

にして、やり直してください。

+0

おそらく正確な推測です。 +1 –

+0

私はまだこのようなメールを受け取ります:お客様のアカウントを確認するには、
?action=3Dverify&id=3D= 33c292bcfc377818a43a1cfb6f470bf91f59e556c221c13a97c326f47c6c00b7のリンクで= をクリックしてください。お楽しみ= ! – john

+0

@john:上記の私の__update__をご覧ください。 – stealthyninja

0

StealthyNinjaの回答にコメントできませんでしたので、自分で投稿しました。申し訳ありません。

質問は少し古くなっていますが、おそらくこれは他人にとって役に立つかもしれません。

HTMLタグと奇妙な文字をすべて取り除くには、電子メールクライアントが電子メールの権利を読み取れるようにヘッダーを準備する必要があります。このが$ヘッダの配列を設定 AFTER試してみてください:

$headers = $message->headers($headers); 

それはそれ以降正常に動作する必要があります。

3

メールヘッダーに問題があるようです。

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

$from = "Sender <*******@googlemail.com>"; 
$to = "Receiver <*******@googlemail.com>"; 
$subject = "Welcome to SITENAME!"; 
$crlf = "\n"; 
$html = "<h1> This is HTML </h1>"; 

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


//$host = "smtp.gmail.com"; 
$host = "ssl://smtp.gmail.com"; // try this one to use ssl 
$port = 465; 
$username = "********@googlemail.com"; 
$password = "********"; 

//$mime = new Mail_mime($crlf); 
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc  
$mime->setHTMLBody($html); 

//$body = $mime->get(); 
$body = $mime->getMessageBody(); //based on pear doc above 
$headers = $mime->headers($headers); 

$smtp = Mail::factory("smtp",array("host" => $host, 
         "port" => $port, 
         "auth" => true, 
         "username" => $username, 
         "password" => $password)); 

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

if (PEAR::isError($mail)) { 
echo $mail->getMessage(); 
} else { 
echo "Message sent successfully!"; 
} 
echo "\n"; 

それはとても私の作品:私は梨メールDOC(http://pear.php.net/manual/en/package.mail.mail-mime.example.php)に基づいて、あなたのコードを更新します私はそれがあなたのために働くことを望む! 乾杯、あなたがHTMLメール用html_charsetを必要とする上記に加えて エレズ

0
$body = $mime->get(array('text_charset' => 'utf-8')); 

$crlf = "\n"; 

$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf)); 

これは電子メールのÂのようなabberationsを修正します。

0

私はこのコードを使用して、後に3Dを削除しました。

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

$mime =& new Mail_mime(); 
$mime->setTXTBody($message); 

if($htmlMessage==""){ 
    $htmlMessage=$message; 
} 

$mime->setHTMLBody($htmlMessage); 
if($attachmentIsFile){ 
    if($attachment!=null) 
     $mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment)); 
}else{ 
    if($attachment!="") 
     $mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false); 
} 
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit')); 
$hdrs = $mime->headers($hdrs); 
関連する問題