I am trying to use PEAR Mail to send from my gmail address using below code,
<?php
include("Mail.php");
echo "This test mail for authentication";
try{
$from_name = "Test";
$to_name = "from name";
$subject = "hai";
$mailmsg = "Happy morning";
$From = "From: ".$from_name." <frommail#gmail.com>";
$To = "To: ".$to_name." <tomail#gmail.com>";
$recipients = "tomail#gmail.com";
$headers["From"] = $From;
$headers["To"] = $To;
$headers["Subject"] = $subject;
$headers["Reply-To"] = "gunarsekar#gmail.com";
$headers["Content-Type"] = "text/plain";
$smtpinfo["host"] = "smtp.gmail.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "mymail#gmail.com";
$smtpinfo["password"] = "mypassword";
//$smtpinfo["debug"]=True;
$mail_object =& Mail::factory("smtp", $smtpinfo);
$mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}catch(Exception $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "<br>Fin";
?>
this code not return any error or warning , it simply shows "Message successfully sent!"
but, mail not receiver to mail the address.
can any one please tell what problem in mycode or what actually happening.,
The first thing I see is that you have a mistake: Your check checks against an variable called $mail, but everything else refers to $mail_object. If that's in your actual code, then I'm guessing that might be part of it.
Some basic checks:
Did you check to make sure that you have POP or IMAP enabled in Gmail?
Have you set up this account with the same username and password on a normal machine, to ensure you can send and receive email outside of PHP?
Verify that you can even talk to the GMail server (that it isn't blocked for some reason) by pinging smtp.gmail.com or using telnet to open a connection to port 25: telnet smtp.gmail.com 25
Read over the Gmail help for sending email.
Beyond that, it looks like GMail requires TLS or SSL, which means you have to use port 587 or port 465. I don't know if that package can handle encrypted connections, though. (Even on port 25, GMail requires SSL encryption.) That may preclude this from working at all.
Related
I have mail function in php which looks like:
<?php
$admin_email = "admin#gmail.com";
$email ="myemail#gmail.com";
$subject = "hellow ord";
$comment = "cmt";
try{
$th=mail($admin_email, $subject, $comment, "From:" . $email);
if($th)
echo "gg";
else
echo "error_message";
}
catch(Exception $e)
{
echo $e->message();
}
?>
I have Wamp server to run it. I have configured hMailServer by seeing a post on Stack Overflow but when I run the above code I get:
Warning: mail(): SMTP server response: 530 SMTP authentication is required...
Do I need to set up anything before using mail function?
The PHP mail() function doesn't support smtp authentication, which generally results in an error when connecting to public servers like the one you are trying to connect to. With hmailserver installed, you still need to use a library like PHPMailer which you can autheticate with.
Follow the steps here to setup phpmailer with gmail:
http://blog.techwheels.net/tag/wamp-server-and-phpmailer-and-gmail/
I've successfully sent email using gmail on (wamp/hmailserver/phpmailer).
<?php
require_once ("class.phpmailer.php");
$sendphpmail = new PHPMailer();
$sendphpmail->IsSMTP();
$sendphpmail->SMTPAuth = true;
$sendphpmail->SMTPSecure = "ssl";
$sendphpmail->Host = "smtp.gmail.com";
$sendphpmail->Port = 465;
$sendphpmail->Username = "your#gmail.com";
$sendphpmail->Password = "gmail_password";
$sendphpmail->SetFrom('your#gmail.com','blahblah');
$sendphpmail->FromName = "From";
$sendphpmail->AddAddress("recipient#gmail.com"); //don't send to yourself.
$sendphpmail->Subject = "Hello!";
$sendphpmail->Body = "<H3>Check out www.google.com</H3>";
$sendphpmail->IsHTML (true);
if (!$sendphpmail->Send())
{
echo "Error: $sendphpmail->ErrorInfo";
}
else
{
echo "Message Sent!";
}
?>
Hope this helps others as well. Took me a few hours of googling and compiling answers from places while facing this issue.
I have a php code for sending confirmation email. But how to send this email to registered user using my mail server. Example using gmail to send confirmarion email.
<?php
if(isset($_SESSION['error'])) {
header("Location: index.php");
exit;
} else {
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$com_code = md5(uniqid(rand()));
$sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());
if($result2) {
$to = $email;
$subject = "Confirmation from MyName to $username";
$header = "TutsforWeb: Confirmation from TutsforWeb";
$message = "Please click the link below to verify and activate your account. rn"; $message .= "http://www.yourname.com/confirm.php?passkey=$com_code";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail) {
echo "Your Confirmation link Has Been Sent To Your Email Address.";
} else {
echo "Cannot send Confirmation link to your e-mail address";
}
}
}
}
?>
If you have mail server then you follow the your mail server rules.
You can use PHPMailer and follow the rule of phpmailer function.
Fix : You have to find out whether you have installed a mail server in your server instance. This depends on server environment. You can find how to with simple web search.
Tip: If just get the response from the operation as normal your mail server is not connected. But if it's keep waiting for like 4 to 5 seconds means most of the time server is there issue is something in it.
Ubuntu - [How to install postfix][1]
Windows - [SMTP E-mail][2]
Further issues :
Once you can send the mail using php mail function but still you have give the accurate header information otherwise the mail will send to spam folder.
Wrong: $header = "TutsforWeb: Confirmation from TutsforWeb";
Correct:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
If you want to do it gmail just refer this : Send email using the GMail SMTP server from a PHP page
You first fetch data from table where registered users are stored then you can send mail to registered users
If you have your mail servers credentials then you can use SMTP to send emails. You can also use PHPMailer which is very easy to use.
First thing is you need to install PHPMailer from above link after that use following code
`
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#gmail.com'; // Your gmail username
$mail->Password = 'your_gmail_password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com'; // from email address
$mail->FromName = 'User1'; // whatever is the name of sender
$mail->addAddress($_POST['email'], $_POST['username']); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message ;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>`
Download PHPMailerAutoload
link here
<?php
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// we are setting the HOST to localhost
$mail->Host = "mail.example.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
$mail->Username = "user#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "from#example.com";
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("to#example.com", "To whom");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$message = "Text Message";
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
i read a lot regarding this issue, and didn't really get clear answer
i simply have local server at home which has xampp and sendmail is working fine... i am using the sendmail folder that comes with xampp and all is fine
i have uncommented the sendmail path address... and put my localhost smtp information, user/pass all ok... works fine
there is another option to
force_sender=test#domain.com
when using this, it sends the email ok, i get in my normal email clinet an email from address: test#domain.com... that is fine
problem is i really want to define the sender name, like comes from MAIL SENDER
something like FROM: "John "
tried with quotes in the force_sender place, no change... i have this mailbox exisited in my xampp (hmail server) and i put the settings there to use FIRST NAME and LAST name like John Smith, but didn't work... all the time just coming like from address format: test#domain.com
this is also similar, but nobody really could help me to clear this doubt and get rest - yet
From address is not working for PHP mail headers
if you want to set sender name than you have to set into in headers. try this
$senderName="John";
$senderEmail= "test#domain.com";
$recipient = "recipient#domain.com";
$subject ="testmail";
$message="test message";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail($recipient, $subject, $message, $headers );
A better approach in php is to use the library phpmailer.
Sending an e-mail would look like this and you can set any fields you want (off course you don't always need that many as in the example).
I guess $mail->FromName = "Your name"; is what you're looking for.
<?php
require '/whereeveritis/PHPMailerAutoload.php';
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "youraddress#gmail.com";
$mail->Password = "yourpasswordinplaintextyeah";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "name#gmail.com";
$mail->FromName = "Your name";
$mail->addAddress("name#example.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Whatever good subject you like to use";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "plain text version";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
I am facing an unexpected issue with a PHP mailfunction. The script sending email to all email address but not to my domain.
Suppose I send email to nitinsoni#gmail.com it was received but when I send email to nitin#mydomain.com it was not received.
I am using GoDaddy web hosting and PHP mail function. SMTP is also not working on GoDaddy server.
PHP code is as follows:
<?php
$to = 'nitin#mydomain.com';
//$to = 'nitinsonitest#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: nitinsonitest#gmail.com' . "\r\n" .
'Reply-To: nitinsonitest#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$r = mail($to, $subject, $message, $headers);
if($r) {
echo 'mail sent';
}else {
echo 'not sent';
}
die;
?>
And SMTP email via PHPMailer is not working as well:
<?php
echo "<pre>";
//die('ada');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
$mail->IsSMTP();
$mail->PORT = 465;
$mail->SMTPDebug=true;
$mail->SMTPAuth = true; // Auth Type
$mail->SMTPSecure = "ssl";
$mail->Username = "user#gmail.com";
$mail->Password = "password";
$mail->Sender = "user#gmail.com";
$mail->From = "user#gmail.com";
$mail->AddReplyTo("user#gmail.com");
$mail->FromName = "user ";
$mail->AddAddress("recepient#gmail.com");
$mail->IsHTML(true);
$mail->Subject = "Test subject";
$mail->Body='Test Subject';
$mail->WordWrap = 50;
if($mail->Send())
{
echo"<script>alert('The Form has been posted ,Thank you');</script>";
}
else
{
echo 'mail error';
}
Suppose I send email to nitinsoni#gmail.com it was received but when
I send email to nitin#mydomain.com it was not received.
I am using GoDaddy web hosting and PHP mail function. SMTP is also
not working on GoDaddy server.
I doubt this has anything to do with the coding when using mail or PHPMailer. The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end—even if it is your domain—has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.
I have a problem I have been working on for about a week and can't find an answer. As a preface to this all, I have searched the internet for all sorts of things. There are a lot of answers for this problem, but none seem to be helping me.
I am somewhat new to PHP and a lot of the stuff I am asking for (been using it over the past few months). Let me get to the base of the problem:
I am on a school network with my own server set up in my dorm room. I am creating a website where I need to verify a user's email, but the basic PHP mail() function does not work. I have been told that I will need to use SMTP. So I decided the easiest and cheapest way was with Gmail SMTP. I created an account on Gmail called verify.impressions#gmail.com for this reason. Here is the code.
echo "starting mail sending";
require_once("pear/share/pear/Mail.php");
echo "1";
$from = "PersonA `<someone#gmail.com`>"; $to = "`<someoneElse#email.com`>"; $subject = "Activate your account"; $body = "Hey";
$host = "ssl://smtp.gmail.com"; $port = "465"; //also tried 587 $username = "someone#gmail.com"; $password = "password";
echo "2";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
echo "3";
$mailer_params['host'] = $host; $mailer_params['port'] = $port; $mailer_params['auth'] = true; $mailer_params['username'] = $username; $mailer_params['password'] = $password;
$smtp = Mail::factory('smtp', $mailer_params);
echo "4";
error_reporting(E_ALL);
echo "5";
if (PEAR::isError($smtp)) { die("Error : " . $smtp->getMessage()); }
echo "6";
$mail = $smtp->send($to, $headers, $body) or die("Something bad happened");
echo "7";
if (PEAR::isError($mail)) {echo($mail->getMessage();} else {echo(Message successfully sent!);}
echo "mail sent hopefully.";
So basically the code just stops at the line:
$mail = $smtp->send($to, %headers, $);
I have tried printing errors, but I just have no idea what to do now. Any tips and help is appreciated. Thanks!!
Use this class: http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html
The sample code I use:
require("smtp/smtp.php");
require("sasl/sasl.php");
$from = 'youraddress#gmail.com';
$to = 'some#email.com';
$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='youraddress#gmail.com';
$smtp->password='XXXXXXXXX';
$smtp->ssl=1;
$smtp->debug=1; //0 here in production
$smtp->html_debug=1; //same
$smtp->SendMessage($from,array($to),array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"));
If you are you using Linux I recommend setting up postfix on your local machine and they asking that to relay the email forwards via an external SMTP service, in your case Gmail.
http://bookmarks.honewatson.com/2008/04/20/postfix-gmail-smtp-relay/
The reason is that your PHP script can timeout incase there's a delay contact Gmail. So you would use Postfix to queue the email on the local server, let the PHP script execution die and trust Postfix to send the email via Gmail.
If you are using Windows, I am sure you can find an equivalent SMTP relay application (should be built as a rough guess).
Many public networks block connections on the SMTP port to remote machines to stop spammers inside their network.
You have two choices:
Find a smtp server that uses a different port than 25
Use the official SMTP server of your school's network. There is always one since people need to send mails out.
I always use this to send mails using gmail as SMTP server.
Very Simple to configure