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

这个代码在我的电脑上运行良好。但在将这个代码/项目上传到服务器后,邮件不会发送。错误为,

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

提示:该服务器中有一个allready正在运行php-symfony2项目,该项目可以成功发送邮件。

这是符号2代码,

$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+Titch的原因是我的主机只有100mb,我需要上传更多的图像。但是符号2占据了更多的空间

看起来您的实时服务器缺少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()函数发送邮件,

这是swift代码。

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

这封邮件的问题是,gmail显示以下消息,

此邮件可能不是由以下人员发送的:username@gmail.com

有时这封邮件会变成垃圾邮件