facebook twitter hatena line email

Php/pear/mail

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

インストール

pear install Mail
pear install Mail_Mime
pear install NET_SMTP

Mailインストール時に'Cannot retrieve channel.xml for channel "pear.php.net" (could not open /var/tmp/channel.xml for writing)'エラが出たとき

sudoつけてインストール

だめなら・・

sudo pear clear-cache
sudo pear update-channels
sudo pear upgrade

NET_SMTPインストール時に'Parsing of package.xml from file "/var/tmp/pearJTUbtI/package.xml" failed'エラが出たとき

pear install Console_GetoptPlus-1.0.0RC1
pear upgrade --force Console_Getopt
pear upgrade --force PEAR-1.10.1
pear upgrade --force PEAR
pear list

Yahooメールでメール送信する場合のサンプル

 require_once('Mail.php');
 require_once('Mail/mime.php');
 $from = 'example1@yahoo.co.jp';
 $params = array(
   'host' => 'smtp.mail.yahoo.co.jp',
   'port' => 587, // No SLL
   // 'port' => 465, // SSL
   'auth' => true,
   'username' => 'example1',
   'password' => 'password1',
 );
 if ($this->_from) {
   $from = $this->_from;
 }
 if (count($this->_params)) {
   $params = $this->_params;
 }
 // shift-jis => JIS
 $original = mb_internal_encoding();
 $subject = mb_convert_encoding( $subject, "ISO-2022-JP", "shift_jis");
 mb_internal_encoding( "ISO-2022-JP" );
 $subject = mb_encode_mimeheader( $subject, "ISO-2022-JP");
 mb_internal_encoding( $original );
 $crlf = "\n";
 $hdrs = array(
   'From' => $from,
   'cc' => $from,
   'to' => $to,
   'Subject' => $subject,
 );
 $mime = & new Mail_mime($crlf);
 if (preg_match("/<html>/", $text)) {
   $mime->setHTMLBody($text);
 } else {
   $text = mb_convert_encoding( $text, "ISO-2022-JP", $code);
   $mime->setTXTBody($text);
 }
 $build_param = array(
   "html_charset" => $code,
   "text_charset" => "ISO-2022-JP",
   "head_charset" => "ISO-2022-JP",
 );
 $body = $mime->get( $build_param );
 $hdrs = $mime->headers($hdrs);
 $address = array($to, $from);
 $mail =& Mail::factory('smtp', $params);
 $result = $mail->send($address, $hdrs, $body);

Yahooメールで以下エラー

authentication failure [SMTP: Invalid response code received from server (code: 535, response: authorization failed (#5.7.0))]

6ヶ月以上使ってないとメールアカウントが削除されるので削除されてないか確認する。

Gmailでメールを送信

アプリ認証でパスワードを生成する(https://security.google.com/settings/security/apppasswords)

$params = array(
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'auth' => true,
    'username' => 'hogehoge@gmail.com',
    'password' => 'hogehogehogehoge',
);