Setting up Mail (PHP) [duplicate] - php

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";
}

Related

Getting error while trying to send mail in php

I am getting the error message as
Failed to connect to mailserver at &quot,localhost&quot, 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.

php jquery contact form without using mail()

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/

How to send email in lotus notes using PHP

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.

Setting up DomainKeys/DKIM in a PHP-based SMTP client [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
It looks like there are some great libraries out there to do DomainKeys signing of emails on C#/.NET, but I'm having a really hard time finding the same kind of support for PHP. Maybe I'm not looking in the right place?
The only one I found is http://php-dkim.sourceforge.net/; it looks incredibly hacky and supports PHP4 only. Considering how popular PHP is, and how critical DomainKeys are for email classification as non-spam, I'd expect better tools; do you know of any? Any other tricks you'd recommend?
Extra info: I'm using an external SMTP provider because I need to send out thousands of emails per day.
I'd recommend DKIM support at the MTA level so all your server generated email for a given domain is signed by default. (unless you have a really good reason to not sign all server generated email for a domain).
The best starting point in my googling to get DKIM setup on LAMP with dkim-milter and sendmail (on CentOS 5.2 in my case) was Jeff Atwood's post about sending emails through code.
I would agree with him that the first 2 things you should address are reverse PTR record and DKIM signing.
Also very important:
IP address of the box to send email not already being blacklisted.
make sure postmaster#emailsendingdomain.com is a valid email box
if your server generated email needs to appear to come from somewhere else (like a contact form needing to come from name/email provided in a form) follow these guidelines for email headers.
Here is the email ip address blacklist checker that I used.
Those 5 things will solve perhaps 95% of your email deliverability issues.
This Guide for Fedora/dkim-milter/postfix is also very good.
The PHP mail library I use for my app is PHPMailer 5.1 which has DKIM support (and is PHP 5 only), but after doing the research, I decided implementing at the sendmail level was a better solution. As you can see, even the author of PHPMailer 5.1 does not suggest DKIM at the PHP mail library level is the best solution http://dkim.worxware.com/.
Best of luck to you.
This is one that has been on my radar for a while and could not find a definitive answer to the original question in this thread anywhere on the web. I have now been able to implement sending DKIM signed SMTP email with PHP/Pear. Below are the steps required.
I found a modified version of the DKIM from http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/ (you can download it via http://www.ra726.net/php-dkim.zip). If you have already implemented DKIM and just need to make it work with SMP mail then all you need from this is the dkim.php file which, as the blog says, is slightly modified to handle headers passed as an array. In my code, I have named it dkimNEW.php.
Ensure you include most headers so that the MTA does not modify the message after you have signed it. In my limited research, the most added headers are the Date and Message-ID headers, thus my header array looks like this: Note: I used this for sending an html email, change to suit! Also, add your domain as the last part of the Message-ID
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'#yourdomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
You will then get to utilize the modified dkim.php mentioned above to sign your email AND add the signature to the headers array, aka
require 'dkimNEW.php';
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
The rest of the code is the normal code to send email via SMTP with PHP/Pear. The full working code is:
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$subject = 'Test of DKIM';
$from = 'My Name <myname#mydomain.com>';
$to = 'First Recipient <recipient1#domain.com>';
$pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
$text = strip_tags($pbody);
// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'#mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
// create the message
$mime = new Mail_mime("\n");
$mime->setTXTBody($text);
$mime->setHTMLBody($pbody);
// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);
require 'dkimNEW.php' ;
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
// create the smtp mail object
$smtp_params = array(
'host' => 'mail.mydomain.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);
// send the message
$recipients = array('recipient1#domain.com', 'recipient2#domain.com');
$mail = $smtp->send($recipients, $headers, $body);
?>
PS. Just in case you did not notice, replace values with your own!
Therefore, all that is essentially needed to make DKIM to work with SMTP email (or indeed the PHP mail) is to ensure that you specify all the headers that are added to your email by your MTA, then sign the headers, subject and body of the message, and finally include that signed portion with your header.
Have you try : phpMailDomainSigner It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.
Here some example:
// Create mailDomainSigner Object
include_once './lib/class.mailDomainSigner.php';
$mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
$new_data = $mds->sign(
$mail_data,
"Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
true,true,false);
A class solely for DKIM which is a spin-off from PHPMailer, but with improvements regarding the respect of the RFC and nice-and-clean code :
https://sourceforge.net/projects/dkim-class-php/
Example :
include_once('dkim.class.php');
$dkim = new DKIM();
$dkim_header = $dkim -> get_DKIM_header($to, $subject, $message, $headers);
mail($to, $subject, $message, $dkim_header.$headers);

Best way to send mass email to my subscribers ( BCC or PEAR mail queue ? )

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

Categories