Powered by eUKhost®

Announcement

Collapse
No announcement yet.

Sending Multiple Emails

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending Multiple Emails

    Since the servers were changed last year I've been using require("class.phpmailer.php"); to send emails via my webpages as required.

    However, I've been unable to successful send a BCC to myself of the email sent to the user.
    See the forum thread:
    http://forums.eukhost.com/f18/phpmai...ficulty-16263/

    I'd also like to be able to use the email addresses in my mysql db to be able to "mail shot" all registered users every now and again. But using phpmailer, I've been able to get it to loop to send the email to multiple recipients.

    Unfortunately, none of the suggestions people have provided have got me any closer to these aims using phpmailer. Is there another option? Surely lots of people must be doing this successfully already? If I can't get the phpmailer thing to work, is there something else instead?

    Many thanks,
    devs

  • #2
    Hi devise,

    Sorry you are facing issues as a result of changes we had made previously. I will forward this to one of our senior executives who will hopefully be able to assist you on this. However, could you elaborate on what exactly happens when you try and BCC yourself using the PHPMailer class? Is there any unexpected output from the PHP interpreter (or from the class)?

    Kind regards,
    Ben.
    Find us on Twitter and Facebook

    Need to contact us?
    Customer Support: Client Area - 0800 862 0380 (option 2)
    Customer Relations: [email protected] - 0800 862 0380 (option 3)
    Sales: [email protected] - 0800 862 0380 (option 1)




    The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.

    Comment


    • #3
      Hope you manage to get it sorted soon. Fingers crossed for you .
      David Smith
      Managing Director
      DPS Computing Limited

      - Massive update! (September 2011) - It's now not neglected!!
      - New Site (10/2009)

      Comment


      • #4
        Thank you.

        I've spent ages playing around with this bit of code at different times. Perhaps that's the trouble - keep on changing things. And also never having a long enough period to sit down and go through it all.

        Well, it seems to be working now. Phew. It's sending BCC's. I'll try putting it on a loop again soon.

        Just to potentially help anyone else in who may be as daft as me to find themselves in this situation...

        The problem doesn't appear to be the code, it appears to be gmail. I'm assuming it sees two identical looking emails and just choses to ignore one. Nothing in the spam box, nothing anywhere else. It just ignores the CC or BCC as it looks the same as the original. Despite it actually being addressed to a different address. (I view both work and personal emails through gmail). So I was looking for two emails and it was ignoring the second one. Having just tried it now with my gf's address & mine it seems to work ok.

        Again, Thank-you for your replies.



        p.s. As a note on something different, I also had problems with gmail which seemed to automatically follow links on unopened emails too. I was sending through a verification link for registration & gmail automatically follows the link before the user even reaches the page. Meaning by the time they reached it they were already registered & it was throwing up errors. blah.
        Last edited by devsie; 08-04-2012, 19:55.

        Comment


        • #5
          Glad that you have managed to find a solution .
          David Smith
          Managing Director
          DPS Computing Limited

          - Massive update! (September 2011) - It's now not neglected!!
          - New Site (10/2009)

          Comment


          • #6
            Getting there...

            Ok, so the AddBCC is now working. Yay.


            Now, i'm just having a little difficulty looping it through to email everyone... (on a different page)

            PHP Code:

            $result 
            mysql_query("SELECT * FROM xxx WHERE active='on' AND emailme1='on'") or die(mysql_error());


            $y=1;


            while (
            $row mysql_fetch_array($result) )
                {

            $title htmlentities($row['title']);
            $surname htmlentities($_row['surname']);
            $forename htmlentities($_row['forename']);
            $name $title." ".ucwords(stripslashes($row['surname']));

            $email htmlentities($row['email']);



            $subject "Newsletter";

            $message "
            Hello 
            $name,<br /><br />

            User specific message.

            All the best,<br />
            ~me"
            ;


            //MAILER FUNCTION
            ob_start();
            require(
            "class.phpmailer.php");

            $mail = new PHPMailer();

            $mail->IsSMTP();                 // set mailer to use SMTP
            $mail->Host XXX;             // specify main and backup server
            $mail->SMTPAuth true;     // turn on SMTP authentication
            $mail->Username XXX     // SMTP username
            $mail->Password XXX      // SMTP password

            $mail->From "[email protected]";
            $mail->FromName "Eye2eye Recruitment";
            $mail->AddAddress($email$name);
            $mail->AddReplyTo("[email protected]""Information");


            $mail->WordWrap 50;                                 // set word wrap to 50 characters
            $mail->IsHTML(true);                                  // set email format to HTML


            $mail->Subject $subject;
            $mail->Body    $message;
            $mail->AltBody "This email is best viewed in HTML.

            "
            .$message;

            if(!
            $mail->Send())
            {
               echo 
            "Message could not be sent. <p>";
               echo 
            "Mailer Error: " $mail->ErrorInfo;
               exit;
            }



            echo 
            $y." Email sent to ".$name." at ".$email."<br />";
            $y=$y+1;




                }        
            // end while loop from mysql search. 

            The page outputs just 1. Email sent to Fred Flinstone at blah blah... But doesn't reach #2 in the database. Or doesn't appear to.
            I considered looping just the "AddAddress->" part, but then I don't see how you could personalise the message content etc.

            Is this another of my dumb moments?

            Many thanks,
            devsie

            Comment


            • #7
              I assume that you have double checked that it is actually connecting to the database successfully? And is it successfully sending the first email? .
              David Smith
              Managing Director
              DPS Computing Limited

              - Massive update! (September 2011) - It's now not neglected!!
              - New Site (10/2009)

              Comment


              • #8
                Hiya,

                Yes it successfully connects to the database, populates the $message with the correct info and sends the first email to me. I'm #1 in the database And then it appears to stop.

                ~devs

                Comment


                • #9
                  Not sure if you have got it working yet - if not try this:
                  Code:
                  	$subject = "Newsletter";
                  	//MAILER FUNCTION
                  	//ob_start(); shouldn't need this
                  	require("class.phpmailer.php");
                  	$mail = new PHPMailer();
                  	$mail->IsSMTP();                 // set mailer to use SMTP
                  	$mail->Host = XXX;             // specify main and backup server
                  	$mail->SMTPAuth = true;     // turn on SMTP authentication
                  	$mail->Username = XXX;     // SMTP username
                  	$mail->Password = XXX;      // SMTP password
                  	$mail->From = "[email protected]";
                  	$mail->FromName = "Eye2eye Recruitment";
                  	$mail->AddReplyTo("[email protected]", "Information");
                  	$mail->WordWrap = 50;                                 // set word wrap to 50 characters
                  	$mail->IsHTML(true);                                  // set email format to HTML
                  	$mail->Subject = $subject;
                  	$result = mysql_query("SELECT * FROM xxx WHERE active='on' AND emailme1='on'") or die(mysql_error());
                  	$y=1;
                  	while ($row = mysql_fetch_array($result) ) {
                  		$title = htmlentities($row['title']);
                  		$surname = htmlentities($_row['surname']);
                  		$forename = htmlentities($_row['forename']);
                  		$name = $title." ".ucwords(stripslashes($row['surname']));
                  		$email = htmlentities($row['email']);
                  		//remove the last addressee if present
                  		$mail->ClearAddresses();
                  		//add the next address
                  		$mail->AddAddress($email, $name);
                  		//and send the email
                  		$message = "
                  			Hello $name,<br /><br />
                  	
                  			User specific message.
                  	
                  			All the best,<br />
                  			~me";
                  		$mail->Body    = $message;
                  		$mail->AltBody = "This email is best viewed in HTML.
                  
                  						".$message;
                  		if(!$mail->Send()) {
                  			echo "Message could not be sent. <p>";
                  			echo "Mailer Error: " . $mail->ErrorInfo;
                  			exit;
                  		}
                  		echo $y." Email sent to ".$name." at ".$email."<br />";
                  		$y++;
                  	}        // end while loop from mysql search.
                  Basic idea is to setup the mailer object first, fill in all the unchanging stuff then go into the loop and add in the address (not forgetting to remove the existing address ) and personalising the message before sending.


                  Hope this helps
                  Regards
                  David
                  David Allen - www.serina.co.uk

                  Comment


                  • #10
                    Thanks for the suggestion, I'm still trying to sort it. I'll try your suggestion (which looks good) as soon as i get time & let ya know how i get on.

                    Thank you

                    Comment


                    • #11
                      Yay.

                      That appears to have worked a treat. Thank you v. much

                      Comment


                      • #12
                        Out of interest...which is the specific line of code that says "send it".
                        Is it the " if(!$mail->Send()) " line ?

                        Comment


                        • #13
                          Originally posted by devsie View Post
                          Out of interest...which is the specific line of code that says "send it".
                          Is it the " if(!$mail->Send()) " line ?
                          Hi Dev,

                          Yes that's correct. 'Host', 'Username' and 'Passwords' are all properties (variables) of the PHPMailer class. In other words, they are variables that the script is assigning the necessary data to send the email. Obviously you would of modified the values to your requirements, such as SMTP information, email to send to, optional attachments, etc. The Send() method/function then executes the logic that sends the actual email itself through SMTP, as set through IsSMTP() method/function. Do note that it is actually executed within an if statement, because of the brackets, which denotes it is a method to PHP, otherwise it would assume (or rather, expect) you are referring to a class variable/property. So evidently the developers of PHPMailer Class have made it so if the email didn't send, it would return FALSE to your application, hence the escalamation mark being before the if statement itself - if(!$mail->Send()) - which means it is checking whether it is false. Which means if the email isn't send successfully, the IF statement block of code is executed. You'll notice at the end the code the exit function is present. This prematurely orders PHP to halt further execution of PHP code (terminate execution), thereby ending the PHP script at the IF statement because of the failed sending of the email for some reason. This means in this case the code at the bottom notifying the end user the email was sent will not be seen. This will be the case in each time the if statement code block is executed ("email not sent").

                          Because PHPMailer is a class, it is using object oriented programming techniques and so a function in a class in PHP would be called a method in most other languages with regards to object-oriented programming (irrespective of ordinary non-object-oriented functions).
                          Find us on Twitter and Facebook

                          Need to contact us?
                          Customer Support: Client Area - 0800 862 0380 (option 2)
                          Customer Relations: [email protected] - 0800 862 0380 (option 3)
                          Sales: [email protected] - 0800 862 0380 (option 1)




                          The opinions or views expressed above are not necessarily the opinions or views of eUKhost Ltd.

                          Comment


                          • #14
                            Hello,

                            Thank you for the very comprehensive reply. That was more than i was expecting.

                            I think it's the fact it's executed within an IF statement that threw me. In my mind it seems strange to have a statement where it's saying "if it doesn't send do this", without having actually already asked it to send. But I have to get used to these short hands in coding.

                            I should probably get around to doing a bit more reading on classes.

                            Many thanks,
                            ~devs

                            Comment


                            • #15
                              Originally posted by devsie View Post
                              I think it's the fact it's executed within an IF statement that threw me. In my mind it seems strange to have a statement where it's saying "if it doesn't send do this", without having actually already asked it to send. But I have to get used to these short hands in coding.
                              Hi - if it helps think of $mail->send() as a function call that returns true on success (I've sent the email) and false on failure (I couldn't send it for some reason).

                              Originally posted by devsie View Post
                              I should probably get around to doing a bit more reading on classes.
                              As an (ahem) 'experienced' programmer, brought up on procedural code and functions, I still sometimes find them hard to follow.
                              David Allen - www.serina.co.uk

                              Comment

                              ban-img
                              Working...
                              X