473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Send email from the server by PHP

hsriat
1,654 Expert 1GB
When a user registers with the website or when he requests to generate a new password, I need to send an email to the user containing some welcome message or his requested information (respectively) as you would have seen on many websites.

What do I need to do for that? I mean, I know the use of mail() function, but what changes do I need to do in php.ini file and how can I configure the email address which will send the emails?

I'm at the verge of project completion, so too tired to do any research.
Can anyone point me to the right direction? Or any tutorials?

Thanks
Apr 30 '08 #1
11 2633
Atli
5,058 Expert 4TB
Hi.

To use the mail() function you would have to edit the [mail function] portion of you php config.

However, there are better alternatives to the mail function, like for example PHPMailer and Swift Mailer.
Apr 30 '08 #2
hsriat
1,654 Expert 1GB
Thank you for the links.
They are really helpful.

:)

Regards,
Harpreet
Apr 30 '08 #3
hsriat
1,654 Expert 1GB
Now what can be the problem with this.. If you have ever used swift mailer.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require_once "lib/Swift.php";
  4. require_once "lib/Swift/Connection/SMTP.php";
  5.  
  6. $swift =& new Swift(new Swift_Connection_SMTP("mail.my_domain.com", 25));
  7.  
  8. $message =& new Swift_Message("Test Mail Subject", "Test Mail Message", "text/html");
  9.  
  10. if ($swift->send($message, "destination_address@yahoo.com", "my_address@my_domain.com"))
  11. {
  12.     echo "Message sent";
  13. }
  14. else
  15. {
  16.     echo "Message not sent";
  17. }
  18.  
  19. $swift->disconnect();
  20.  
  21. ?>
It shows "Message not sent" every time.
Jun 20 '08 #4
Atli
5,058 Expert 4TB
Are you using PHP4?
Swift uses Exceptions, which PHP4 does not support. If you were using PHP5, an exception should have been thrown, showing you what the problem is.

Try something like this. It should show you what is causing the error.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Swift_Errors::expect($e, "Swift_ConnectionException");
  3. $result = $swift->send(...);
  4.  
  5. if(!$result) {
  6.   if($e != null) {
  7.     echo "Failed: ". $e->getMessage();
  8.   }
  9.   else  {
  10.     echo "Failed: Unknown error.";
  11.   }
  12. }
  13. else {
  14.   echo "Sucess!";
  15. }
  16. ?>
  17.  
Jun 20 '08 #5
hsriat
1,654 Expert 1GB
Fatal error: Class 'Swift_Errors' not found

I'm using PHP 5.
There is no class as Swift_Errors. There's a Swift_Exception class, but it contains no method.
Jun 21 '08 #6
Atli
5,058 Expert 4TB
No, Swift_Errors is specific to PHP4 only.
The PHP5 version throws exceptions so there is no need for it.

Try something like this.

Btw, if you are using PHP5, there is no need to pass new object by reference. PHP does it automatically.
Jun 21 '08 #7
hsriat
1,654 Expert 1GB
Now it always says "Not sent"
[PHP]<?php

require_once "swift/Swift.php";
require_once "swift/Swift/Connection/SMTP.php";

try
{
$swift =& new Swift(new Swift_Connection_SMTP("mail.my-domain.in"));

$message =& new Swift_Message("Test Mail Subject", "Test Mail Message", "text/html");

$is_Sent = $swift->send($message, "destination@yahoo.com", "no-reply@my-domain.in");

if ($is_Sent) echo "Sent";

else echo "Not sent";

}

catch (Swift_ConnectionException $e)
{
echo "There was a problem communicating with SMTP: " . $e->getMessage();
}

catch (Swift_Message_MimeException $e)
{
echo "There was an unexpected problem building the email:" . $e->getMessage();
}

$swift->disconnect();

?>[/PHP]
Jun 21 '08 #8
hsriat
1,654 Expert 1GB
Ok, I changed my mind and used PHP Mailer.
I sent a mail 4 hour ago to my yahoo address, and I just got it now.
Jun 21 '08 #9
Atli
5,058 Expert 4TB
Ok, I changed my mind and used PHP Mailer.
I sent a mail 4 hour ago to my yahoo address, and I just got it now.
That would probably be due to Yahoo's hyperactive spam filters.
I would guess that your mail was flagged and queued for some advanced filtering, which will probably take a while for a huge service like Yahoo.

But if the mail got there eventually, your mail script is working as it should.
Jun 21 '08 #10
Atli
5,058 Expert 4TB
Now it always says "Not sent"
That's very weird...

I was under the impression that Swift would throw exceptions if something, like sending, failed in PHP5.

According to that, you should never get the "Not sent" message, but rather the exception.

You didn't get any errors or warnings or anything like that?
Jun 21 '08 #11
hsriat
1,654 Expert 1GB
That's very weird...

I was under the impression that Swift would throw exceptions if something, like sending, failed in PHP5.

According to that, you should never get the "Not sent" message, but rather the exception.

You didn't get any errors or warnings or anything like that?
No error message, just 'not sent' thing.

But now I'm not working on that anymore. PHPMailer worked in the first try. So I'll stick to it.

And I understand that that 4 hours issue is not my script's problem but the intermediate servers' delay. Moreover my hosting server sux, I'll find another one at the end of this annual contract.

Thank you Atli :)

Harpreet
Jun 22 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Google Mike | last post by:
I've got RH9 Linux with default PHP. Is there a way to send email on Linux to an Exchange Server from PHP and/or other tools when there is *NOT* SMTP access? Has anyone figured out a way to...
40
by: ian | last post by:
Hi, I'm a newbie (oh no I can here you say.... another one...) How can I get Python to send emails using the default windows email client (eg outlook express)? I thought I could just do the...
6
by: DigitalRick | last post by:
I have been running CDONTS in my ASPpages to send emails to me sent from my guestbook. It had been working fine untill I upgraded to Server 2003 (I am also running Exchange 2003) all locally. I...
9
by: Bob Jones | last post by:
We have developed a commercial ASP.net application (personal nutrition management and tracking); we want to send smtp email from within it. For our development box, we use WinXP Pro, IIS 5.5,...
2
by: Ron | last post by:
hi guys, I am trying to send email using smtpMail. I can send emails inside the organization, but out of the organization I get an error "The server rejected one or more recipient addresses. The...
3
by: RN | last post by:
I am tired of sending mail from the built-in SMTP service for so many reasons (errors are nondescriptive in the event log, it doesn't let me control which IP address it sends from, and it...
14
by: supz | last post by:
Hi, I use the standard code given below to send an email from an ASP.NET web form. The code executes fine but no Email is sent. All emails get queued in the Inetpub mail queue. I'm using my...
2
by: oyster | last post by:
I find that the existing email moudle is some hard for me to understand, especially the part of how to set the CC, BCC and attach the files. Is there any more easy one like this p-code? import...
3
by: YMPN | last post by:
Hi Everyone, I'm deen from Riyadh. Please do help me with some problem i have. I have this formview control setup to recieved inputs from user (textbox, dropdownlist, others). After...
9
by: Mahernoz | last post by:
Can i send an email from JavaScript? Is it possible? If yes please the code to send email using javascript...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.