trying to send mail using swift mailer, gmail smtp, php

后端 未结 8 1225
攒了一身酷
攒了一身酷 2020-12-01 01:08

Here is my code:

setUs         


        
相关标签:
8条回答
  • 2020-12-01 01:16

    I have managed to get this working without the SSL, here is how:

    $transport = Swift_SmtpTransport::newInstance('tls://smtp.gmail.com', 465)
                ->setUsername('contact@columbussoft.com')
                ->setPassword('password');
    $mailer = Swift_Mailer::newInstance($transport);
    
    $message = Swift_Message::newInstance($subject)
                ->setFrom(array($emailTo=>$name))
                ->setTo(array($emailTo=>'Neo Nosrati'))
                ->addPart($body,'text/plain')
                ->setReturnPath('other@columbussoft.com');
    
    0 讨论(0)
  • 2020-12-01 01:19

    I'm using the "Messages Swift Mailer" bundle in Laravel 3 and having the same issue. After some testing, in my case, the solution was to set the same email address that I used in the SMTP authentication on the "from" parameter.

    I was trying to use a different address and that was triggering the "swiftmailer expected response code 220 but got code with message" error.

    Hope that helps.

    0 讨论(0)
  • 2020-12-01 01:21

    Swift SmtpTransport - Code (send a email)

    The SMTP of GMAIL is: smtp.googlemail.com

    The Full Code:

    <?php
    $pEmailGmail = 'xxxx@gmail.com';
    $pPasswordGmail = '********';
    $pFromName = 'MundialSYS.com'; //display name
    
    $pTo = 'xxxxxx@xxxx.xxx'; //destination email
    $pSubjetc = "Hello MundialSYS"; //the subjetc 
    $pBody = '<html><body><p>Hello MundialSYS</p></html></body>'; //body html
    
    $transport = Swift_SmtpTransport::newInstance('smtp.googlemail.com', 465, 'ssl')
                ->setUsername($pEmailGmail)
                ->setPassword($pPasswordGmail);
    
    $mMailer = Swift_Mailer::newInstance($transport);
    
    $mEmail = Swift_Message::newInstance();
    $mEmail->setSubject($pSubjetc);
    $mEmail->setTo($pTo);
    $mEmail->setFrom(array($pEmailGmail => $pFromName));
    $mEmail->setBody($pBody, 'text/html'); //body html
    
    if($mMailer->send($mEmail) == 1){
        echo 'send ok';
    }
    else {
        echo 'send error';
    }
    ?>
    
    0 讨论(0)
  • 2020-12-01 01:24

    there is missing the ssl parameter, it should be something like that

    Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
    

    Tested and work fine

    0 讨论(0)
  • 2020-12-01 01:26

    I cannot be sure, but I think that Gmail's port is 587 using TLS, which is not SSL, but a newer version of it. You should check into that, because I think you are placing the wrong construction code.

    Best of luck!

    0 讨论(0)
  • 2020-12-01 01:30

    For google apps, in addition to setting to port 465 and ssl as recommended in the accepted answer, you may have to enable allow less secure apps setting, as per https://stackoverflow.com/a/25238515/947370

    0 讨论(0)
提交回复
热议问题