Got issue with Swift Mailer Not sending messages

我们两清 提交于 2020-01-02 08:38:33

问题


I have an issue with swift mailer, it is not sending message to the user I extracted the library to inc folder in my website and created the following message for swift mailer to send:

NOTE: If you can suggest other solutions besides SwiftMailer, could you please leave a comment.

require_once 'inc/lib/swift_required.php';

    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('mail.mywebsite.com', 25)
    ->setUsername('info@mywebsite.com')
    ->setPassword('myPassword')
    ;

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

    //Define Message Subject
    $message = Swift_Message::newInstance('myWebsite Registration')

    //e-mail from
    ->setFrom(array('info@mywebsite.com' => 'myWebsite'))
    //e-mail to
    ->setTo(array('$email1' => '$username'))
    //e-mail body
    ->setBody('

    Message body Here

    ')
    ;

    $result = $mailer->send($message);

    //Show user message after message has been sent
    include_once 'msgToUser.php'; 
    exit();

I tested it and I didn't get a message and I don't understand why, I followed instructions at official website: http://swiftmailer.org/docs/sending.html

Can anyone suggest what the problem is?

NOTE: I have used PHP mail(); function before to send e-mails, but they were classified as spam. If possible can anyone suggest solution how to use mail(); and make e-mails not SPAM? Please don't suggest something like: use your host server instead of your domain, e.g. instead of using info@yourdomain.com use info@yourhost.com.


回答1:


I fear that your question can not be specifically answered, because next to concrete technical problems you can face complex problems here that need multiple places to be touched which is not trivial.

The Sending Issue

First of all, you can send emails with SwitfMailer, this should "just work"tm. However, if the transport you're using, like the SMTP server is not delivering the emails, that is not related to SwiftMailer directly (as it wouldn't be related to PHP mail).

However, you can use the $result from SwitfMailer to find out if SwiftMailer itself was running into any errors sending the email. See SwiftMailer SMTP Troubleshooting (Archived Copy) for some more info in case you've got problems to get SwiftMailer to work with your SMTP server.

The Spam Issue

This can not be specifically answered. Similar questions pop up here on SO from time to time, but most often things are not specifically answered because this topic stretches over what exact content has your email (email header and body Spam scoring), from which server it is send (IP-Address and Hostname based Spam scoring, e.g. blacklists as well as DNS configuration for the Hostname, see SPF) and other stuff.

Using SwiftMailer can help as you can exchange mailer backends, e.g. to buy-in a commercial SMTP gateway that focusses to lower the burden of your mails scored as Spam. However as I wrote it in some other answer/comment: A whole business is running on spam - on the "bad" and the "good" side. So it's not always advisable to trust any of these entities.

Instead you can start to learn about how email technically works, how spam scoring is working and what you can do to send correct emails over a properly configured SMTP server. That is some work and it can not be provided as an answer here because it's very broad.




回答2:


Make sure your server allows SMTP connections and if it does, check that it is on port 25 and not using SSL.

Then, wrap your code on a try/catch block to catch any exception.

try {
echo '<pre>';
    $transport = Swift_SmtpTransport::newInstance('mail.mywebsite.com', 25)->setUsername('info@mywebsite.com')->setPassword('myPassword');
    $mailer = Swift_Mailer::newInstance($transport);
    $message = Swift_Message::newInstance('myWebsite Registration')
        ->setFrom(array('info@mywebsite.com' => 'myWebsite'))
        ->setTo(array('$email1' => '$username'))
        ->setBody('Message body Here');
    $result = $mailer->send($message);
print_r($result);
} catch (Exception $e) {
    echo $e->getMessage();
}



回答3:


Try checking your mailbox, when he mailbox exhausted it's quota, it wont send any email, i got my SMTP mail account sending emails again as soon as i clean out the mailbox. I set the mailbox quota through CPANEL planning that no one will send replies to this box, but the email recipients sent replies to this mailbox and made the mailbox full.



来源:https://stackoverflow.com/questions/7777968/got-issue-with-swift-mailer-not-sending-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!