开发者

Got issue with Swift Mailer Not sending messages

开发者 https://www.devze.com 2023-04-13 10:03 出处:网络
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:

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::newInstanc开发者_如何学Goe('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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号