PHPMailerでGmailのメール送信
現在、WordPress(2.7.1)でサイトを作成しています。
お問い合わせフォームやコメント通知としてGoogle Appsのメールアドレス宛に送信しようとしています。
ここで問題が!
さくらインターネットのレンタルサーバにサイトを作っているのですが、sendmailコマンドでは送信できませんでした。
/usr/sbin/sendmailコマンドを使用してもdead.letterファイルに追加されるばかりで、いっこうに送信できません。
そこで、Google Apps(gmail)のメールサーバを使用して送信するようにしました。
WordPressではPHPMailerを使用しているのですが、次のように修正することで送信できるようになりました。
まず、wp-includes/pluggable.phpをエディタで開き、次のように修正します。
381行目あたりの$phpmailer->IsMail();を$phpmailer->IsSMTP();に変更します。
// Set to use PHP's mail() //$phpmailer->IsMail(); $phpmailer->IsSMTP();
そして、wp-includes/class-phpmailer.phpを開き、165行目から始まるSMTP設定の変数を次のように変更します。
///////////////////////////////////////////////// // PROPERTIES FOR SMTP ///////////////////////////////////////////////// /** * Sets the SMTP hosts. All hosts must be separated by a * semicolon. You can also specify a different port * for each host by using this format: [hostname:port] * (e.g. "smtp1.example.com:25;smtp2.example.com"). * Hosts will be tried in order. * @var string */ var $Host = 'smtp.gmail.com'; /** * Sets the default SMTP server port. * @var int */ var $Port = 465; /** * Sets the SMTP HELO of the message (Default is $Hostname). * @var string */ var $Helo = ''; /** * Sets connection prefix. * Options are "", "ssl" or "tls" * @var string */ var $SMTPSecure = "ssl"; /** * Sets SMTP authentication. Utilizes the Username and Password variables. * @var bool */ var $SMTPAuth = true; /** * Sets SMTP username. * @var string */ var $Username = 'hogehoge@example.com'; // <= Google Appsのメールアドレス /** * Sets SMTP password. * @var string */ var $Password = 'hogehoge'; // <= パスワード
注意すべき点として、この設定で送信すると、Fromが$Usernameで指定したアドレスから送信されることになります。
Google Appsだけではなく、Gmailアカウントもこの設定でいけると思います。