Jump to content

Pear? Sending Email With Mail() Using Different Smtp


djp1988

Recommended Posts

I am finding it hard understanding just how I can set up my script to send email using my smtp rather then simply use the mail() function, it seems to me unreliable and I'd like to use an smtp that I use for my email.I downloaded PEAR but I could get it working, can anyone help me? How can I set this up ?

Link to comment
Share on other sites

Okay maybe I should also say that I'm on a shared hosting plan on GoDaddy, so I don't have access to the server or any command lines to deal with the server, is phpmailer an easier solution ?Can anyone just give me an example of script that can use an smtp of my choice? to send email ?

Link to comment
Share on other sites

mail() isn't necessarily unreliable, but yeah, it only uses the SMTP server specified in the php.ini config file. However, if you are on Windows you can use ini_set() to specify another value at runtime: http://www.php.net/manual/en/mail.configuration.php#ini.smtp.The lowest-level way to send an email using an arbitrary SMTP server is to open a direct socket connection and just send the mail manually. PHPMailer can do this.

$from = "me@domain.com";$to = "you@remote.com";$subject = "Test email";$message = "This is a test message.";$smtp = fsockopen("smtp.server.com", 25);fwrite($smtp, "EHLO domain.com\r\n");fwrite($smtp, "MAIL FROM:$from\r\n");fwrite($smtp, "RCPT TO:$to\r\n");fwrite($smtp, "DATA");fwrite($smtp, "From:$from\r\nTo:$to\r\nSubject:$subject\r\n\r\n");fwrite($smtp, $message);fwrite($smtp, ".");fclose($smtp);

Link to comment
Share on other sites

Apparently the SMTP option is "used under Windows only" - Linux systems use sendmail, and I don't think the sendmail settings can be modified from PHP.

Link to comment
Share on other sites

Ah so, indeed. You can set the sendmail path, but that's about it. You could add it to the command line for sendmail though:

$cmd = '/usr/sbin/sendmail -oi';$fh = popen($cmd, 'w');  	  if (!$fh)	  {		error_log('Error opening sendmail process');	  }	  else	  {		$headers = 'To: ' . $to . "\r\n";		$headers .= 'Subject: ' . $subject . "\r\n";		$headers .= 'From: ' . $from . "\r\n";  		fputs($fh, $headers);		fputs($fh, $message);  		$result = pclose($fh) >> 8 & 0xFF;  		if ($result != 0)		{		  error_log('Error closing sendmail process: ' . $result);		}	  }

Not sure what the command line option for it is though.Apparently it's the -p option$cmd = '/usr/sbin/sendmail -oi -psmtp:smtp.domain.com';

Link to comment
Share on other sites

how can i use the phpmailer script to send with my smtp using authentication? I am really lost about all this.At the moment I am here: I have "installed PEAR" and run the go-pear.php and now have the package manager - why ? I don't give a damn... And using all the examples pear provided and what i have seen in forums and on about.com I can't get my email to send... I thought it was simple to start out, I downloaded some files from the pear website including a Mail.php file and a bunch of other files which were extensions to the Mail class in Mail.phpBut it could find a file called Net/SMTP.php on line 311 of smtp.php

include_once 'Net/SMTP.php';

From that point onwards I have been frustratingly searching for a solution but have nothing to show for it... :) All I want to do is set my emails sent from php to use a different smtp so I won't have to worry about my smtp relay limit... :)

Link to comment
Share on other sites

PEAR is a package manager. PEAR_Mail, for example, is a package. When PEAR gets installed on a server it creates a directory for it's own include files and adds the directory to PHP's include path. Your include statement is looking in the Net subdirectory inside the PEAR include directory for the SMTP package. That statement will work if PEAR is installed correctly on the server.I highly doubt that GoDaddy will let you install something like a PEAR package if they don't already have it. These aren't things you install on your computer and then upload with your PHP code, these are things that need to be installed on the server. Create this file and upload it:

<?phpinclude_once 'PEAR.php';include_once 'Mail.php';include_once 'Mail/mime.php';echo '<pre>' . print_r(get_declared_classes(), true) . '</pre>';?>

If you see include errors, or if you don't see the PEAR classes listed in the list of declared classes, then contact GoDaddy and ask them if and how you can use PEAR packages. If you can't or don't want to use PEAR then try one of the other methods.

Link to comment
Share on other sites

I think I may have my answer, here is how my php ws compiled by goaddy:

Configure Command	 './configure' '--enable-fastcgi' '--prefix=/usr/local/php5' '--with-config-file-path=/web/conf' '--disable-posix' '--enable-bcmath' '--enable-calendar' '--with-curl=/usr/bin/curl' '--with-gdbm' '--enable-exif' '--enable-ftp' '--with-gd' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr/bin/libpng-config' '--enable-gd-native-ttf' '--with-gettext' '--with-mcrypt=/usr/bin/libmcrypt-config' '--with-mhash' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl' '--with-pdo-mysql=/usr' '--with-pspell' '--enable-soap' '--enable-wddx' '--with-xsl' '--with-zlib' '--enable-mbstring' '--enable-zip'

I don't see a --with-pear so should I stop looking down the PEAR road?

Link to comment
Share on other sites

Maybe so. Try the code above, that will let you know if PEAR is installed.For what it's worth, the best thing I've seen about PEAR is the package installer, which actually works pretty well. Once it's actually installed the documentation is incomplete or sparse, the examples are minimal or inconsistent, and it's not always clear why something doesn't work. I was troubleshooting using Mail_Queue to send out batch emails yesterday and I had to read through the PEAR source to figure out what the problem was. Even when I knew that it wasn't able to connect to the database, the code I had looked exactly like their example which supposedly worked, but I had to change it to something else that wasn't documented very well in order to get it to connect.I especially liked seeing this one. Here's their mail queue example:http://pear.php.net/manual/en/package.mail...ue.tutorial.phpWhich includes this:$db_options['dsn'] = 'mysql://user:password@host/database';In the source for the queue class they have this comment:* //optionally, a 'dns' string can be provided instead of db parameters.And then this is the error checking code: function Mail_Queue_Container_db($options) { if (!is_array($options) || !isset($options['dsn'])) { return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_OPTIONS, $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__, 'No dns specified!'); }Apparently they aren't even clear if they're using a data source name or the domain name system.Some things in PEAR are alright to use, but if they put the effort to put a little polish on the thing it would make it a lot better.

Link to comment
Share on other sites

Okay I'm trying to use phpmailer now, I feel I'm getting somewhere easier, but I' still having problems.my script says I couldn't connect to the server:

 Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.live.com:25 (Connection refused)

But I don't understand why, I gave it the correct server, port number, username, password and set it to do ssl and use authentication.I noticed the error came from line 122, which is:

	$this->smtp_conn = fsockopen('ssl://smtp.live.com',	// the host of the server								 '25',	// the port to use								 $errno,   // error number if any								 $errstr,  // error message if any								 $tval);   // give up after ? secs

But looking at that, it would seem it's trying to connect to that socket but without yet using the authentication username/password, could this be the problem ?What would you suggest I do ?I have not modified the phpmailer files, and my script is the following:

require_once('phpmailer/class.phpmailer.php');$mailer = new PHPMailer();$mailer->IsSMTP();$mailer->Host = 'ssl://smtp.live.com:25';$mailer->SMTPAuth = TRUE;$mailer->Username = 'my_email@hotmail.fr';  // Change this to your gmail adress$mailer->Password = 'my_pass';  // Change this to your gmail password$mailer->From = 'contact@mysite.com';  // This HAVE TO be your gmail adress$mailer->FromName = 'dan'; // This is the from name in the email, you can put anything you like here$mailer->Body = 'This is the main body of the email';$mailer->Subject = 'This is the subject of the email';$mailer->AddAddress('different_email@site.com');  // This is where you put the email adress of the person you want to mailif(!$mailer->Send()){   echo "Message was not sent<br/ >";   echo "Mailer Error: " . $mailer->ErrorInfo;}else{   echo "Message has been sent";}

Link to comment
Share on other sites

Are you sure the host is meant to be written ssl://smtp.live.com:25? Generally, since protocol is assumed, "smtp.live.com" should do (also, ssl:// is not technically a protocol). Also, SMTP over SSL doesn't use port 25 by default, it uses port 465.Also, are you sure the live.com SMTP server supports SSL?

Link to comment
Share on other sites

I am using this SMTP from my Mail.app on my computer, so it does allow remote connections, I tried using port 465 and that doesn't work either.About the ssl:// I saw in a forum post somewhere about GMail working with phpMailer, and it needed to be using ssl, and they added ssl:// so I guessed it was the same for my smtp, as iot also needs to use ssl

Link to comment
Share on other sites

Try it with the Gmail SMTP server, see whether that works (though putting ssl:// before the server name is strange, and using port 25 for SSL SMTP is even stranger).For all it's worth, I use the Hotmail SMTP server to send messages, and I connect to it over port 587, and with TLS (not SSL) - it works for me!

Link to comment
Share on other sites

Do you use phpmailer? if so can I get a look at one of your scripts connecting you to the smtp please? password/username hidden of course

Link to comment
Share on other sites

No, I'm using Hotmail with a desktop email client. But try something like

	$this->smtp_conn = fsockopen('smtp.live.com',	// the host of the server								 '587',	// the port to use								 $errno,   // error number if any								 $errstr,  // error message if any								 $tval);   // give up after ? secs

Link to comment
Share on other sites

If that doesn't work, try Zend_Mail, from the Zend Framework, like:

<?phprequire_once 'Zend/Loader/Autoloader.php';Zend_Loader_Autoloader::getInstance();Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp('smtp.live.com', array('auth' => 'login','username' => 'myusername','password' => 'password','ssl' => 'tls','port' => 587)));$mail = new Zend_Mail();$mail->setFrom('my_email@hotmail.fr', 'djp')	->addTo('somebody_else@example.com', 'someone else')	->setSubject('TestSubject')	->setBodyText('This is the text of the mail.')	->send();?>

If even THAT doesn't work, try to configure your email for Outlook first, then see if there's a difference in the port, username or something else. If there are no differences, try other 'auth' methods, and try both 'ssl' and 'tls' as values for 'ssl' at each 'auth' method.

Link to comment
Share on other sites

Okay after much frustration, I tested one of my previous scripts on the localhost and it works ! So it's a problem with godaddy, wow, it seems that I am finding many small disadvantages with them then you would expect...

Link to comment
Share on other sites

Well, maybe smtp.live.com blocks connections from GoDaddy, you never know. Try opening a direct socket connection, or if you have SSH access just telnetting to it.

Link to comment
Share on other sites

I asked godaddy and got this quite pointless response to my question, I asked if something blocks connecting to other smtp servers as I could get my script to work, and they didn't say if so, they only told me to use their, limited smtp

Thank you for contacting Online Support.If you are using a script (ASP, PHP, JSP, etc) that nеed to ѕеnd еmаils оut, you must sреcify the following server to send out from:relay-hosting.secureserver.netPlease note, this ѕerver аllоwѕ 1000 rеlayѕ. Theѕе relays cannot be reset or increased.Please let us know if we can assist you in any other way. Sincerely,Online Support Team

Link to comment
Share on other sites

I asked godaddy and got this quite pointless response to my question, I asked if something blocks connecting to other smtp servers as I could get my script to work, and they didn't say if so, they only told me to use their, limited smtp
Thank you for contacting Online Support.If you are using a script (ASP, PHP, JSP, etc) that nеed to ѕеnd еmаils оut, you must sреcify the following server to send out from:relay-hosting.secureserver.netPlease note, this ѕerver аllоwѕ 1000 rеlayѕ. Theѕе relays cannot be reset or increased.Please let us know if we can assist you in any other way. Sincerely,Online Support Team

Well, that's still somewhat helpful... if you want to send emails from addresses like noreply@yourdomain.com (instead of my_email@hotmail.fr), you can use that server. This would be useful if you want to send "cofirmation" or "newsletter" emails of a sort.And FYI, when dealing with GoDaddy, I'd expect many more small disadvantages, that when summed lead to big ones.
Link to comment
Share on other sites

At the moment I'm using mail() with "FROM: no-reply@mydomain.com" in the headers string, so I guess using the secureservers relay I'm still going to get the same effect right ? I still feel cheated as when I got the godaddy plan they never say what you don't get ! And it's only once you NEED something that you realise they don't offer it

Link to comment
Share on other sites

At the moment I'm using mail() with "FROM: no-reply@mydomain.com" in the headers string, so I guess using the secureservers relay I'm still going to get the same effect right ?
Yes, though some servers will mark this as spam, because the mail isn't coming from where it claims to be from.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...