2

swiftmailer を使用して PC からメールを送信できますが、メールがサーバーに送信されません。

私はswiftmailer 5.0.1を使用しています。プロジェクトの詳細は、

  1. netbeans での単純な php プロジェクト
  2. スウィフトメーラー 5.0.1
  3. 小枝1.13.1

私のコードは

public function init() {
        $this->username = 'username@gmail.com'; 
        $this->password = 'password';
        $this->host = 'ssl://smtp.gmail.com';
        $this->port = 465;
        $this->from = 'username@gmail.com';
        $this->subject = 'Company - contact';
        $this->body_part_type = 'text/html';
    }

public function send_email($from_name_add, $to_add, $fullname, $email, $mobile, $content) {
            $this->init();
            $transport = Swift_SmtpTransport::newInstance($this->host, $this->port)
                    ->setUsername($this->username)
                    ->setPassword($this->password);

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

            $message = Swift_Message::newInstance();
            $cid = $message->embed(Swift_Image::fromPath('../public_html/pic/logo.png'));
            $this->body = $this->renderEmailTemplate('email', $fullname, $email, $mobile, $content, $cid);
            $message->setSubject($this->subject)
                    ->setFrom(array('username@gmail.com' => '' . $from_name_add))
                    ->setTo($to_add)
                    ->setContentType($this->body_part_type)
                    ->setBody($this->body);
            $result = $mailer->send($message);
            return $result;
        }

このコードは、私の PC で正常に動作します。しかし、このコード/プロジェクトをサーバーにアップロードした後、メールが送信されません。エラーは、

    <br />
<b>Fatal error</b>:  Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host ssl://smtp.gmail.com [Connection timed out #110]' in /home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php:259
Stack trace:
#0 /home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer-&gt;_establishSocketConnection()
#1 /home/am***/lib/Swift/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer-&gt;initialize(Array)
#2 /home/am***/lib/Swift/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport-&gt;start()
#3 /home/am***/controller/send_mail.php(54): Swift_Mailer-&gt;send(Object(Swift_Message))
#4 /home/am***/public_html/contact.php(43): send_mail-&gt;send_email('Am*** Inc', 'fe****@gma...', 'asdf', 'asdf@in.com', '111111111111', 'Testing mail')
#5 {main}
  thrown in <b>/home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php</b> on line <b>259</b><br />

ヒント:そのサーバーには既に実行中の php symfony2 プロジェクトがあり、このプロジェクトはメールを正常に送信できます。

これが symfony2 コードです。

$message = \Swift_Message::newInstance()
                ->setSubject($sub)->setFrom($from)->setTo($to)->setContentType("text/html")
                ->setBody($this->renderView('FZAm***Bundle:Layout:mail.html.twig', array
                    ('name' => $this->fullname, 'mobile' => $this->mobile, 'email' => $this->email,
                    'content' => $this->content, 'time' => $this->sys_time, 'ip' => $userip,
                    'server_time' => date('Y-m-d H:i:s'))
        ));
try {
            $this->get('mailer')->send($message);
// catch and other follows. 

構成の詳細は、

mail_contact_from: username@gmail.com
  mail_contact_to:   username@gmail.com
  mail_contact_sub:   Contact info

この詳細のみを渡し、すべての設定がデフォルトです。情報が必要な場合は、投稿してください。

symfony2 プロジェクトからこの通常の php+swift+twig に変更する理由は、ホスティングが 100 MB しかなく、さらに画像をアップロードする必要があるためです。しかし、symfony2 はより多くのスペースを占有します。

4

2 に答える 2

4

グーグルで検索した後。

gmail 認証を使用してメールを送信するには、このコードを使用する必要があります。

$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465);

これが正しいメール送信方法です。このメールはgmailで送信されます。


PHPは mail() を使用してメールを送信します。

mail($to,$subject,$message,$headers);

このコードは、php コードを使用してメールを送信します。

Swiftmailer は、この php の mail() 関数を使用してメールを送信することもできます。

これが迅速なコードです。

$transport = Swift_MailTransport::newInstance();
// nothing inside ()

このメールの問題は、gmail が次のメッセージを表示することです。

このメッセージは、username@gmail.com から送信されたものではない可能性があります。

このメールがスパムに分類されることがあります。

于 2013-08-08T17:40:02.340 に答える
4

ライブ サーバーが見つからないOpenSSLようです。安全な接続を機能させるには、ライブ サーバーを有効にする必要があります (つまり、php_opensslモジュールを有効にします)。この質問もチェックしてください。

于 2013-08-08T15:17:06.407 に答える