Welcome! Log In Create A New Profile

Advanced

Sending email

Posted by CHARLCROW-72771437 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Sending email
March 21, 2011 09:43AM
Hi guys, just wanted to know how you guys have managed to send email with PHP so far, this is the one thing I am struggling with.
I changed the details in the php.ini file to the smtp details for gmail, double checked if they were correct and the mail failed.


I then downloaded PHPmailer script which is apparently the best option to use if you are a windows user, I set up everything and my mails are still not going through
I keep getting a failed sent, has anyone on a windows 32-bit OS managed to succeed in sending mails from a localhost.

This is my code

Language: PHP
<?php require_once('includes/PHPmailer/class.phpmailer.php'); require_once ('includes/PHPmailer/class.smtp.php');       $to_name = 'First Last'; $to = 'recipient@domain.com'; $subject = 'Mail Test at '. strftime('%T',time()); $message = 'This is a test'; $message = wordwrap($message,70);   $from_name = 'First Second; $from = 'email@domain.com';   $mail = new PHPMailer();   $mail -> IsSMTP(); $mail -> Mailer = 'smtp'; $mail -> SMTPAuth = true; $mail -> SMTPSecure = 'ssl'; $mail -> Host = 'smtp.domain.com'; $mail -> Port = 465; $mail -> Username = 'emailI@domain.com'; $mail -> Password = 'password';   $mail -> FromName = $from_name; $mail -> From = $from; $mail ->AddAddress($to,$to_name); $mail ->Subject = $subject; $mail ->Body = $message;     $result = $mail->Send;   echo $result ? 'SENT' : 'FAIL '.$mail -> ErrorInfo;   ?>
Re: Sending email
March 21, 2011 11:40AM
I have managed to get it working using

link

as a guide. Note read it fully as there is an extension in one of the latter posts.

I now get an email on every failure.

--

Student Number: 7298-786-3
avatar
Mac
Re: Sending email
March 21, 2011 04:18PM
You should not worry too much about mail not being sent. XAMPP is a local installation so it cannot physically send mail since it is not connected to the Internet.
PHPMailer is what I use myself.

Note the use of ' and "

Language: PHP
$to_name = 'First Last'; //versus $to_name ="$fname $sname";
The latter you are likely to use, so it needs to be "
Re: Sending email
March 21, 2011 05:35PM
Thank you Mac and Wsbraun.

Mac do you recommend that we use " instead of ' with all of our code, I've been using ' and my code has been working as expected?
avatar
Mac
Re: Sending email
March 22, 2011 07:06AM
A string in double quotes is parsed by PHP.

Language: PHP
echo $sname; // The value of $name is printed echo '$sname'; // The word '$name' is printed echo "$sname"; // The value of $name is printed e.g. Johnny   //Concatenation   $sname = 'Johnny';   echo 'Your name is ' . $sname; // Prints Your name Johnny echo "Your name is $sname"; // Prints Your name Johnny

Your choice.

However, you may want to print a URL

Language: PHP
echo "<a href="http://www.there.com">Go to There</a>"; //parse error echo "<a href=\"http://www.there.com\">Go to There</a>"; //escape double quotes with a \ echo '<a href=\"http://www.there.com"\>Go to There</a>'; // wil not work - will print what is between the single quotes, including the escape characters and double quotes echo "<a href='http://www.there.com'>Go to There</a>"; // will also work but is rules incorrect and lazy echo "<a href=http://www.there.com>Go to There</a>"; and echo '<a href=http://www.there.com>Go to There</a>';// will also work but is slack coding and strict browsers will eventually start nailing you
Re: Sending email
March 22, 2011 03:03PM
Thanks Mac, will definitely note that
avatar Re: Sending email
March 22, 2011 07:23PM
I have previously managed to send mail via PHP by using the PHP Mail function, you can see it being used on w3schools

"I like nonsense, it wakes up the brain cells."
Re: Sending email
March 23, 2011 04:48PM
Bellyheart what operating system do you use, getting the mail() function to simply work on a windows machine localhost is a shlep, well for me at least.
avatar Re: Sending email
March 24, 2011 04:22PM
mac Wrote:
-------------------------------------------------------
> You should not worry too much about mail not being
> sent. XAMPP is a local installation so it cannot
> physically send mail since it is not connected to
> the Internet.
> PHPMailer is what I use myself.
>

Any suggestions on how we can test to see if our code works?

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar
Mac
Re: Sending email
March 25, 2011 09:19AM
Use a real server... unfortunately. FWHA allows you to free websites with php and mysql.
Re: Sending email
March 25, 2011 06:28PM
Hey guys,

Thought I'd add my 2c:

Language: PHP
$smtp_server_win = "196.36.104.67";   //Form data variables $name = $_POST['fname'] ; $surname = $_POST['sname'] ; $contact = $_POST['cell'] ; $email = $_POST['email'] ; $query = $_POST['query'] ;   if (isset( $smtp_server_win ) && strlen($smtp_server_win)) { ini_set( 'SMTP', $smtp_server_win );};   date_default_timezone_set("Africa/Johannesburg");   if ($name!=' '){ mail( "address@domain.com", "Website Query", "Client's Name: $name\nClient's Surname: $surname\nClient's Contact number: $contact\nClient's e-mail address: $email\nClient's Query: $query", "From: query@domain.com" ); };

This is a small script I slapped together for a work project.
I had to specify the outgoing server since our hosting server is also used for a type of "Secure mail."
Not sure how neat or "legal" this script is though
This is hosted on a Windows Webserver 2008
avatar Re: Sending email
April 20, 2011 12:08PM
Could anyone perhaps tell me why this piece of code has quite literally sent me over 2000 emails lol

Language: PHP
$student_info = "SELECT fname, sname, email, contact_flag FROM student"; $result = mysql_query($student_info) or die ("Could not retrieve student info for mail: " . mysql_error());   while($row = mysql_fetch_array($result)){ $fname = $row['fname']; $sname = $row['sname']; $email = $row['email']; $contact = $row['contact_flag']; $text .= $row['fname'] . ' ' . $row['sname'] . ' - ' . $row['email']. "\n";       $from = 'From: Info <info@spargweb.co.za>' . "\r\n"; $to = $row['email']; $subject = "Successfully Registered"; $body = "Hi $fname $sname,\n\nHere is a list of all students currently registered.\n\n$text"; if(mail($to, $subject, $body , $from)) { header("location:student_man.php"); } }

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar Re: Sending email
April 20, 2011 07:47PM
I have no idea why it sent you so many emails! Maybe its sending you one for each pass of the loop?

I am struggling a bit. I have managed to send an email to the email in the registration form saying you have registered for $course. Am now struggling with trying to use the output of a while loop as the message of the email! I am struggling a bit with loops as a whole actually. Anyways, the code I tried is

Language: PHP
$SQL5 = "SELECT course_student.sno, student.fname, student.sname, student.email FROM student LEFT JOIN course_student ON course_student.sno=student.sno WHERE course_student.cid=$cid";   $result5 = mysql_query($SQL5);   while ($row5 = mysql_fetch_array($result5)) { $msg = $row5['fname'] . " " . $row5['sname'] . " " . $row5['email'] . "\n"; }   $to = $email; $subject = "Registation"; $message = $msg; $from = "stefan.wiswedel@gmail.com"; $headers = "From:" . $from;   mail($to,$subject,$message,$headers);

If I actually use that on a server then I just get an email with the name, surname and email address of the student busy registering. If instead of creating the variable $msg, I simply
Language: PHP
echo $row5['fname'] . " " . $row5['sname'] . " " . $row5['email'] . "\n"
then it actually prints all the names, surnames and emails of all the students registered for the same course selected in the form.

What gives? Any ideas or suggestions?

Shaun, I might try put my mail script within the loop like u have it but worried that will just send a whole loop's worth of emails. hehe
avatar Re: Sending email
April 20, 2011 08:00PM
Found the solution on the post on loops. Goes something like

Language: PHP
$msg .= $row5['fname'] . " " . $row5['sname'] . " " . $row5['email'] . "\n"

Thanks WSBRAUN!!!
avatar Re: Sending email
April 20, 2011 08:58PM
I took it out the loop cause I was getting spammed by my own code lol

Thing is the email has to send an email to every student registered for that course every time someone new registers right? Or does the newly registered student only receive it?

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar Re: Sending email
April 20, 2011 09:00PM
Oh ya, supposed to send to every student! Thanks for reminding me smiling smiley
avatar Re: Sending email
April 20, 2011 09:10PM
I think mine is working. So hard to test winking smiley gets so confusing with all these emails flying around! At least I have 2 email addresses to work with. Home and work smiling smiley
avatar Re: Sending email
April 20, 2011 09:13PM
So far I can send an email to the last registered student only, still trying to figure out how to send it to all

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar Re: Sending email
April 20, 2011 09:36PM
Yeah, my code is currently

Language: PHP
while ($row5 = mysql_fetch_array($result5)) { $student_list .= $row5['fname'] . " " . $row5['sname'] . " " . $row5['email'] . "\n"; $all_emails .= $row5['email'] . ","; }   $to = $all_emails; $subject = "New students added to" . " " . $course; $message = "Here is a full list of all students registered for" . " " . $course . " " . "that are happy to be contacted." . "\n \n" . $student_list; $from = "stefan.wiswedel@gmail.com"; $headers = "From:" . $from;   mail($to,$subject,$message,$headers);

When I echo my $all_emails variable it seems to print it out properly but finding it so hard to see if it actually comes through as is. Getting a bit of delay and lag with emails coming through etc.. ERGH! So not sure if it is or isn't working sad smiley
avatar Re: Sending email
April 20, 2011 09:49PM
Unless I'm doing something wrong in mine but

Language: PHP
$email .= $row['email'].",";

still only sends it to one student

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar
Mac
Re: Sending email
April 21, 2011 09:19AM
Mmm, can't see absolutely no problem with that code..... the error is elsewhere

Run the code here below and see what you get.

Language: PHP
$five=mysql_query("SELECT * FROM students order by sname ASC"); $nummail = mysql_numrows($five); $i = 0; while ($i<$nummail) { $five_fname= mysql_result($five,$i,'fname'); $five_sname= mysql_result($five,$i,'sname'); $five_email .= mysql_result($five,$i,'email').","; $i++; }   $to = $five_email; echo "$to";

But rather get to know phpmailer - the inbuilt php mail function is heavy on the server.
avatar Re: Sending email
April 21, 2011 09:23AM
This is an error

Language: PHP
$nummail = mysql_numrows($five);

supposed to be

Language: PHP
$nummail = mysql_num_rows($five);

smiling smiley

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar
Mac
Re: Sending email
April 21, 2011 09:30AM
mysql_numrows is deprecated but may still be used for backward compatibility spinning smiley sticking its tongue out

But by all means use mysql_num_rows!
avatar Re: Sending email
April 21, 2011 09:45AM
Apologies, I didn't know smiling smiley

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar Re: Sending email
April 23, 2011 05:55PM
Can someone tell me why this only sends an email to 1 recepiant and not all please

Language: PHP
$num = mysql_num_rows($result); $i = 0; while($i < $num){ $row = mysql_fetch_array($result); $mail = $row['email'];   $fname = mysql_result($result, $i, 'fname'); $sname = mysql_result($result, $i, 'sname'); $email .= mysql_result($result, $i, 'email').","; $text .= $fname . ' ' . $sname . ' - ' . $mail. "\n"; $i++; }

Thanks

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar Re: Sending email
April 24, 2011 07:31PM
Hey Shaun,

Sorry, not good enough yet to spot these things without actually trying out the code and troubleshooting it. What I did with the emailing was just echo the results to make sure it was outputting the emails as neccessary for the email script (i.e. commas between email addresses and correct spacing etc...).

Sorry I can't be of much more help! If I get a moment later or tomorrow I'll come back to it.
Re: Sending email
April 25, 2011 08:09AM
Shaun use print_r() just to check the contents of the array you going to iterate through, its a nice way to debug. Also first get everything from user table into an array then use a while
loop to loop through each user and mail each one.
avatar Re: Sending email
April 25, 2011 08:27AM
CHARLCROW-72771437 Wrote:
-------------------------------------------------------
> Shaun use print_r() just to check the contents of
> the array you going to iterate through, its a nice
> way to debug. Also first get everything from user
> table into an array then use a while
> loop to loop through each user and mail each one.

Thanks, got my mail working, it just won't email the same email to multiple recipients, if I echo out the $to variable it has all the mail addresses though, so I'm confused.

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
Sorry, only registered users may post in this forum.

Click here to login