Swiftmailer配置:使用gmail发送邮件 [英] Swiftmailer config : send mail using gmail

查看:151
本文介绍了Swiftmailer配置:使用gmail发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用swiftmailer 5.0.1发送电子邮件。项目细节是


  1. netbeans中的一个简单的php项目

  2. swiftmailer 5.0.1

  3. twig 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 ='公司 - 联系人';
$ 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 ($这 - >密码);

$ 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>致命错误< / b> ;:未捕获的异常'Swift_TransportException'与消息'连接无法建立与主机ssl://smtp.gmail.com [连接超时#110]'在/ home /am*** / lib / Swift / class / 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' ,'测试邮件')
#5 {main}
throw n在< b> / home / am *** / lib / Swift / classes / Swift / Transport / StreamBuffer.php< / b>在< 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('Ymd H:i:s'))
));
尝试{
$ this-> get('mailer') - > send($ message);
// catch等等。

配置详细信息是,

  mail_contact_from:username@gmail.com 
mail_contact_to:username@gmail.com
mail_contact_sub:联系信息

我只传递了这些细节,所有设置都是默认设置。如果需要任何信息,请问我的帖子。



原因我正在从symfony2项目改为这个普通的php + swift + twig是我的主机只有我需要上传更多的图像。但是symfony2占用更多空间。

解决方案

看起来你的实时服务器缺少 OpenSSL ,所以您需要启用它才能使安全连接工作(即启用 php_openssl 模块)。另请查看此问题


I can send email from my pc using swiftmailer, but mail not sending in server.

I'm using swiftmailer 5.0.1. Project details are,

  1. A simple php project in netbeans
  2. swiftmailer 5.0.1
  3. twig 1.13.1

My code is

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;
        }

This code works FINE in my pc. But after upload this code/project to server, mail not sending. Error is,

    <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 />

HINT: There is an allready running php symfony2 project in that server, this project can send mail successfully.

Here is the symfony2 code,

$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. 

Config details are,

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

I passed only this details and all settings are default. If any info needed please ask i'l post.

Reason i'm changing from symfony2 project to this ordinary php+swift+twig is my hosting is only 100mb and i need to upload more images. But symfony2 occupy more space.

解决方案

Looks like your live server is missing OpenSSL, so you need to enable it in order to get secure connections working (i.e. enable the php_openssl module). Also check this question.

这篇关于Swiftmailer配置:使用gmail发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆