Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@meso_

Composerがない環境でSwiftMailerを利用する

More than 3 years have passed since last update.

ソースを入手

Gitから入手

  • libを作業ディレクトリにアップロードする

config設定

$config = array(
    'host' => 'smtp.example.com',
    'port' => 465,
    'user' => 'dummy@example.com',
    'pass' => 'password',
    'encryption' => 'ssl',
    'url' => 'http://localhost',
    'mailaddress' => 'dummy@example.com',
    'sendname' => '株式会社Dummy担当',
    'subject_user' => 'お問い合わせありがとうございます',
    'subject_admin' => 'お問い合わせを受信しました',
    'note' => true,  // 送信控え
    'cc' => array(
        'red@example.com',
        'blue@example.com',
    ),
    'bcc' => array(
        'green@example.com',
    ),
);

送信処理

    // 日本語に関する初期設定
    Swift::init(function () {
        Swift_DependencyContainer::getInstance()
            ->register('mime.qpheaderencoder')
            ->asAliasOf('mime.base64headerencoder');
        Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
    });

    // SMTPサーバーとの接続設定
    $transport = Swift_SmtpTransport::newInstance($config['host'], $config['port'])
        ->setUsername($config['user'])
        ->setPassword($config['pass'])
        ->setEncryption($config['encryption']);

    $mailer = Swift_Mailer::newInstance($transport);


    // メール情報をセット
    $subject = sprintf($config['subject_admin'], $posts['nameKanji'], $posts['postKind']);
    $body = $body_header_admin.$mail_data['body_middle'].$body_footer_admin;
    $from = array($config['mailaddress'] => $config['sendname']);
    $to = array($config['mailaddress'] => $config['sendname']);

    // メールの作成
    $message = Swift_Message::newInstance();
    $message->setCharset('iso-2022-jp');
    $message->setEncoder(Swift_Encoding::get7BitEncoding());
    $message->setSubject($subject);
    $message->setFrom($from);
    $message->setTo($to);
    if (count($config['cc']) != 0) {
        $message->setCc($config['cc']);
    }
    if (count($config['bcc']) != 0) {
        $message->setBcc($config['bcc']);
    }
    $message->setBody($body, 'text/plain');

    // 送信処理
    $result = $mailer->send($message);

    // 送信控え送信
    if ($config['note'] == true) {
        $subject = $config['subject_user'];
        $body = $body_header_user.$mail_data['body_middle'].$body_footer_user;
        $to = array('dummy@example.com' => 'dummy');

        $message = Swift_Message::newInstance();
        $message->setCharset('iso-2022-jp');
        $message->setEncoder(Swift_Encoding::get7BitEncoding());
        $message->setSubject($subject);
        $message->setFrom($from);
        $message->setTo($to);
        $message->setBody($body, 'text/plain');

        // 送信処理
        $result = $mailer->send($message);
    }

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
meso_

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?