I've been trying to follow a tutorial to make a contact form - http://www.tutwow.com/htmlcss/create-a-simple-and-secure-contact-form-with-jquery-and-php/
The only problem is that it uses mail() which my host has disabled for 'security reasons'. I tried to make my own contact form a while back without fancy validation using smtp which worked fine, is there any easy way to use this tutorial example with smtp easily?
I'm quite clueless with php but I think I would need to change this from the tutorial:
mail($to, $subject, $message, $headers);
To something like:
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
Am I even close? Any help would be great, thanks.
Most of PHP developers use PHPMailer if they want to ensure their script runs completely.
Try it out ...
You could use the Zend_Mail_* classes. You can configure your SMTP very easily.
Setting up SMTP http://framework.zend.com/manual/en/zend.mail.sending.html
The documentation for the Zend_Mail class http://framework.zend.com/manual/en/zend.mail.html
Alternative is SwitfMailer http://swiftmailer.org/
Related
I am getting the error message as
Failed to connect to mailserver at ",localhost", port 25,...
Contents of my php.ini file are
SMTP = localhost
smtp_port = 25
I used the following code
mail("xyz#gmail.com","test","msg","from abc#gmail.com");
You can't send outbound mails from localhost. To test the mail function, install mercury mail. It should come with xampp. Create emails for the localhost domains like steward#localhost. YOu could use aliases. Do your testing with that sending mails from one inbox to the other. You'd need a licensed version of mercury mail to send outbound messages.
Another option is to run your test on a remote server. Make sure the senders email is recognised by the sending server. Like you cannot be sending a gmail message using those settings you are displaying.
If sending from gmail is your objective, stackoverflow is full of answers already for how to send mails with gmail, even with codeigniter. Need some?
Here is what you ask for: Sending mail using gmail:
require_once "Mail.php";
$from = "<from.gmail.com>";
$to = "<to.yahoo.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myaccount#gmail.com"; //<> give errors
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?> <!-- end of php tag-->
Get info here: https://stackoverflow.com/a/2748837/827224
You could even do more if you are using codeigniter or any PHP email class like PHPMailer
search email classes in phpclasses.org. An example is:
http://www.phpclasses.org/blog/package/9/post/1-Sending-email-using-SMTP-servers-of-Gmail-Hotmail-or-Yahoo-with-PHP.html
Finally, here is a class you can use:
http://www.phpclasses.org/package/7834-PHP-Send-e-mail-messages-Gmail-users-via-SMTP.html
Its clearly evident that there is no mail server running on localhost on port 25 (or a firewall is blocking it). Get and install a mailserver (there are a number of free ones for windows/linux/mac - just make sure your ISP allows it) and your script will run just fine.
I use this for testing which is quite nice: http://smtp4dev.codeplex.com/
Its a fake email server, that intercepts mail and dumps them for you to inspect and test on localhost.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Send email using GMail SMTP server from PHP page
I'm pretty new to mail in PHP, and I am wanting to set up mail().
The problem is, after a few hours of trying to get it working, I simply can not!
I am wanting this to happen:
I want to send emails to the users on my website via my gmail address.
I am not sure WHERE i configure SMTP for gmail. Do i edit the settings in php.ini (ssl:smtp.gmail.com; 465)?
Is there a way to send emails using the PHP mail() function without the need to use something like pear? I am just wanting to use the mail() function.
If that is not possible, is there a way I can send emails to my users via the localhost setup?
I am pretty confused after looking around for answers during the past few hours.
Any help would be greatly appreciated!
The easiest way I've found to get PHP to send mail using SMTP is via the Mail Pear package.
This way you don't have to involve and obese third party libraries like PHPMailer.
Here's an example:
<?php
require_once "Mail.php";
$headers = array(
'From' => "Sandra Sender <sender#example.com>",
'To' => $to="Ramona Recipient <recipient#example.com>",
'Subject' => "Hi!"
);
$smtp = Mail::factory('smtp', array(
'host' => "ssl://smtp.gmail.com",
'port' => 465,
'auth' => true,
'username' => "smtp_username",
'password' => "smtp_password"
));
$body = "Hi,\n\nHow are you?";
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo $mail->getMessage();
}
else {
echo "mail sent successfully";
}
Need to send email using PHP via lotus notes. Notes is configured on my system. So just wanted if I could send email using PHP. Can anybody help with the code and configuration that I am supposed to do?
After reading replies from all of you, I tried to nail down things from my end. I could at least move one step ahead with all your help. I could figure out my mail server using GetEnvironmentString and its damn correct as also reflected in my lotus notes work space. But when I am trying to use the below code it just keeps on loading and finally returning nothing -
<?php
require_once "Mail.php";
$from = "abc#email.com";
$to = "abc#email.com";
$subject = "Test!";
$body = "Hi,\n\nTest?";
$host = "d23abcd";
$port = "1352";
$username = "abc#email.com";
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Am I committing some mistake here? I doubt
$host = "d23abcd";
$port = "1352";
If your Lotus Domino server has SMTP set up, you can use the Domino server as outgoing mail server (if PHP is able to send mail using a relay server).
Thanks a bunch for all your responses and replies. Finally, I am able to send mail using domino server. Would like to share few things that I came across -
Using $session->GetEnvironmentString("MailServer",True); figured out the server where session is an instance of COM object for Notes.NotesSession like new COM( "Notes.NotesSession" );
Secondly, I was trying with port 1352 which I got from netstat command for this particluar server process. But it didnt work and finally worked on 25 only.
Domino server was not accepting authentication, so used mail($to,$subject,$message,$headers); instead of $mail = $smtp->send($to, $headers, $body);
Happy that it worked. Thanks all for the help and suggestions.
Using your local Notes Client or a Notes Client installed on a "server" via COM to send mail is not a good idea. What you want is to send email from PHP via an SMTP server (which can be a Domino server, as Per pointed out).
Sending email via PHP is for example explained here and here. For the name of the server, the port used for SMTP and optional credentials, please contact your local Domino admin.
I've set up a simple mail system, which resembles the following:
$from = 'me <me#me.com>';
$to = 'you <you#you.com>';
$subject = 'subject';
$body = 'body';
$host = 'www.me.com';
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::Factory('smtp', array('host' => $host, 'auth' => true,
'username' => 'username', 'password' => 'password'));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
throw new Exception('emailException');
}
When I try run the script however the browser (chrome) hangs indefinately. There is no exception thrown, no error page shown by the browser, it just sits and waits for a response (for over 12 minutes, longest I've left it).
My actual program is slightly different (the code is enclosed in a function with info passed as parameters). I have used static debugging and confirmed that the parameters getting passed in are correct, however it just never errors, so I can't even test any error information in $mail.
Does anyone know how I should go about debugging this?
Update I can confirm the page hangs when calling $smtp->send(...)
Debugging this could be done in the logfiles, there you might find some PEAR errors.
Futhermore you could debug by echo statements between each line of code so you know exactly where it hangs.
Long delays are likely to incur when trying to connect to a non existing host. So that's where I would start searching although 12 minutes is very very long. You need to use the PEAR class because you need to send through a designated SMTP server?
IME, more than 99% of reported errors regarding PHP's mail functionality are problems with mail and neither PHP nor it's interface.
Why are you trying to connect directly to the server instead of using PHP's mail()?
Do you have shell access to the machine? It makes the following simpler however you can do these from PHP.
Can you resolve the smtp server hostname?
Can you connect to port 25 on the smtp server?
How is authentication implemented? (should be included in the response to your EHLO)
If you've got shell access then route the connection via a logging proxy or capture usnig tcpdump/wireshark and see where the connection is getting stuck.
Most likely the mailserver is not implementing proper SMTP and the authentication is going awry.
I need to send email to my 5000 subscribers.
What is the best way to do this ?
1) By using BCC ?:
$from_addr = 'myemail#example.com';
$mailing_list = 'sub1#example.com', 'sub2#example.com', 'sub3#example.com0;
$message_subject = 'this is a test';
`$headers = array ("From" => $from_addr,
"Bcc" => $mailing_list,
"Subject" => $message_subject);
$smtp = Mail::factory("smtp", array ('host' => "smtp.example.com",
'auth' => true,
'username' => "xxx",
'password' => "xxx"));
$mail = $smtp->send($email, $headers, $message_body);`
.
2) by using PEAR mail queue ?
I haven't used PEAR mail_queue yet, but using a queue is definitively the way to go!
BCC shouldn't be used because your mails would easily get flagged as Spam by big email providers like gmail/hotmail.
Also having thousands of addresses in an email header seems to be crazy. There may even be a limit. Also some mail servers could refuse your mail because of the over-sized header. On top of that the mail server that is supposed to send your email wouldn't be to happy about it.
Using built-in mail function is not the best way in the first place for that. I would suggest you to go for SwiftMailer which has HTML support, support for different mime types and SMTP authentication which is less likely to mark your mail as spam.
Also, you can check out this pear package:
http://pear.php.net/package/Mail_Queue