Results 1 to 18 of 18

Thread: [Code Example] Sending SMTP Emails using fsockopen()

Threaded View

  1. #9
    New Member
    Join Date
    Mar 2012
    Posts
    7

    Re: [Code Example] Sending SMTP Emails using fsockopen()

    Hi Bertie,

    Thanks for the update - that's great.
    And I am glad that I am beginning to understanding things correctly.

    By the way, I am running a dedicated server and have a good relationship with the
    server provider who also my ISP - They know I am working on this project to send
    out loads ( thousands ) of emails. So there is no problem with that.

    So the mail server is on mail.mydomain.com

    Like you, I prefer to understand the whole code rather than relying on other libraries
    cos no matter how well they are tested, if something goes wrong, I wont be able
    to know where to look.

    You said:

    For example if the mail server doesn’t support EHLO a library
    Is that the mail server library on my server ?
    Or do you mean on the server that my server is sending the emails to ?

    If it is my server , then I could just find out what is supported and use that.
    However - having said that, if its only a few lines of extra code to drop down to HELO
    - then we may as well include them ( just in case ).

    This timeout - does that mean after a specified number of seconds and no
    response comes, we kill the connection ?
    Or does it mean total time connection can be open for ?


    Multiple SMTP sending connections
    Not sure how this works, but I have been doing some research:

    The software will rotate between each SMTP sending connection available
    — this allows users to spread out the sending load among multiple SMTP servers,
    which can offer considerable benefits with regards to performance and deliverability.
    On my dedicated server, I have several websites. As far as I know each can have its
    own has is own SMTP sending connection.

    So if I have a 3 virtual servers set up as
    "my-domain.com"
    "my-second-domain.com"
    "my-third-domain.com"

    Then I could open an smpt connection on each one ?

    I will have to ask my ISP if I have any restrictions - so far they have not said I do, and
    because I was asking if my processor and bandwidth was enough, they would have said so.
    Anyway I will check again.

    But this is interesting:


    If you have more than one email sending connection set up, you can edit each
    connection to change the “Emails Per Cycle” setting. (Note: you must have at least two
    connections set up before you see this option.) Emails per cycle determines how many
    emails to send with the given connection before moving on to the next connection. By
    default, it is set to 50 emails per connection. This setting is very useful, as it allows
    you to distribute more emails with a certain server than with another, if you wanted to do so.

    For example, let us say that you have 5 SMTP servers, and each server can only send 600
    emails per hour, but you want to send as quickly as possible using all 5 of these SMTP connections.

    What you should do if you wanted to send as quickly as possible using all of your connections
    would be to set the sending throttling settings to 3000 emails per hour, and set the Emails Per Cycle
    to 600. This would send 600 emails with “Connection 1? at a rate of 3000/hour, which would take
    about 12 minutes to do. Then it would move on to “Connection 2,” and send another 600 emails
    within 12 minutes. It would then move on to “Connection 3,” etc. By the time the software
    has rotated through all five connections, one hour will have passed, and approximately 3000
    emails will have been sent out, but none of the SMTP connections will have gone over their limit.

    So it looks to me like they send the "Cycle" number of emails, like 600 ,in one connection before
    closing it.

    Now, one thing about this I am not sure about.
    If the software is only using one connection at a time,
    then I do NOT see any advantage. Only if there is a limitation on the
    SMTP servers sending rate is there an advantage.

    But - can three SMPT servers be opened and used concurrently ?

    If so, then that would give an advantage.

    Although my IPS wrote this:

    I would say your biggest throttle wouldn't be your server or bandwidth but the servers you send to. Your server can send a large amount of mail very fast but the receiving ends all have limitations in place to prevent abuse on their networks. Some people have legitimate emails that need to go to a large portion of users on a domain. In these cases I would recommend contacting some of the big names such as gmail, hotmail and yahoo to see what their policy is and throttle your email according to their networks. Then for the less commonly known networks take the lowest throttle and make that the default until you start seeing issues with mail going through. This will of course take a decent amount of debugging on your part but the end result will be very good since all of your emails will be delivered successfully.
    I also asked my IPS:
    Is my current connection broad enough for me to ramp up the service ?

    Their answer:
    Yes, we don't limit your bandwidth but please be careful as you have a 2000GB cap per a month. If you somehow exceed that in a month the overage charges are $1/GB, however most customers never even break 100GB in a month so I think you will be ok.

    So, from what they have written, additional SMPT connections
    may not be of any benefit ???

    Now, if I should use 3 connections:-

    I have a "queue" table which holds all the emails that need to be sent.
    There could be 3000-5000 emails in the queue.

    Here is an idea of how it could work:

    1) Read in 100 emails from the "queue" table
    2) Open 1st Connection
    3) Check it opened successfully. If not ( what ? try again or try a different connection ?
    4) Call the send function: Attempt to Send email with all the checking and log results in array.
    4a) write the logarray to logfile.txt
    5) Do (4) until end of 100 batch
    5 a ) Update the queue and subscription tables using SQL query on logfile.txt


    6) Read in next 100 emails from the "queue" table
    7) Open 2nd connection
    8) Check it opened successfully. If not ( what ? try again or try a different connection ?
    9) Call the send function: Attempt to Send email with all the checking and log results in array.
    9 a) write the logarray to logfile.txt
    10) Do (9) until end of 100 batch
    10 a ) Update the queue and subscription tables using SQL query on logfile.txt

    11) Read in next 100 emails from the "queue" table
    12) Open 2nd connection
    13) Check it opened successfully. If not ( what ? try again or try a different connection ?
    14) Call the send function: Attempt to Send email with all the checking and log results in array.
    14 a) write the logarray to logfile.txt
    15) Do (14) until end of 100 batch
    15 a ) Update the queue and subscription tables using SQL query on logfile.txt


    But because I only have 3 smpt connections, I need to loop
    back and use the first connection again.

    Note:
    I am not sure if writing the logarray to a logfile.txt with fwrite()
    is faster than a dbase query update or not.
    Doing hundreds of SQL queries could slow down the process.

    Because it seems that a simple fwrite() should be faster, I thought to write the txt file then
    use it to update the queue and subscription tables with one SQL query after processing
    each 100 email batch.

    What do you think?
    Have you used multiple smpt connections at all - do you know if it is a good idea ?

    If I only use one connection it will make the programming easier

    Can you see any other problems with this approach ?

    Hopefully this discussion is useful to you as well.
    (it is very helpful to me )


    Thanks again.



    .
    Last edited by Davvit; Mar 17th, 2012 at 08:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width