繁体   English   中英

Swiftmailer配置:使用gmail发送邮件

[英]Swiftmailer config : send mail using gmail

我可以使用swiftmailer从我的电脑发送电子邮件,但邮件不能在服务器中发送。

我正在使用swiftmailer 5.0.1。 项目详情是,

  1. netbeans中的一个简单的php项目
  2. swiftmailer 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;
        }

这段代码在我的电脑上运行FINE。 但在将此代码/项目上传到服务器后,邮件不发送。 错误是,

    <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是我的主机只有100mb,我需要上传更多图像。 但symfony2占据更多空间。

看起来您的实时服务器缺少OpenSSL ,因此您需要启用它才能使安全连接正常工作(即启用php_openssl模块)。 还要检查这个问题

在谷歌搜索一些。

要使用gmail auth发送邮件,您需要使用此代码,

$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

有时这封邮件会发送垃圾邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM