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
Related
Attempting to build a mass-texting (multiple cellphone recipients) code, using an html form and a php engine.
Side note: My pastor sends a daily text (using a cellphone app) to 300+ subscribers, but only some arrive. Some only receive one or two a month. Often he sends to me 5 to 10 times a day before I get one.
The "answers" I've seen for similar issues just confuse me more. I am a novice; I do not even completely comprehend the instructions for asking questions.
<!DOCTYPE php 5.3 PUBLIC >
<head>
<!---
// Double slash indicates comments
// This page url = http://edwardcnhistianchurch.edwardnc.org/Test-Kitchen/Mass_text/text_engine.php
// Form url = http://edwardcnhistianchurch.edwardnc.org/Test-Kitchen/Mass_text/text.html
--->
<Title>Text Engine</Title>
<src="http://edwardchristianchurch.edwardnc.org/Test-Kitchen/Mass_Text/default.config.php">
</head>
<?php
// Define variables
$EmailFrom = "2524025303#mms.uscc.net" ;
// Add additional addresses in next line 'enclosed' and separated by commas
$EmailTo = "2529169282#vtext.com,2524025305#mms.uscc.net, ";
$Subject = Trim(stripslashes($_POST['Subject']));
$Body = ($_POST['smsMessage']);
$From = Trim(stripslashes($_POST['From']));
$Password = Trim(stripslashes($_POST['Password']));
// <!--- SMTP server = yew.arvixe.com ; domain = mail.edwardnc.org --->;
$host = "yew.arvixe.com";
$username = "2524025305#edwardnc.org";
$SMTP_authentication = "Normal_Password";
$password = $Password;
$port = "587";
// SMTP Configuration
// enable SMTP authentication
$mail->SMTPAuth = true;
$mail->Host = $host;
$mail->Username = $username;
$mail->Password = $password;
$mail->Port = $port;
$mail->From = $EmailFrom;
$additional_parameters = '$mail' ;
// SendEmail
// $success = mail($EmailTo, $Subject, $Body, "From: <no_reply#edwardnc.org>" );
// Next line requires STMP_Authentication, line above works on another page;
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" );
// Indicate success or failure
if ($success){
print "Message was sent to multiple recipients" ;
}
else {
print "OOPS! Something went wrong";
}
?>
</src="http://edwardchristianchurch.edwardnc.org/Test-Kitchen/Mass_Text/default.config.php">"
Warning: mail() [function.mail]: SMTP server response: 530 SMTP >authentication is required. in E:\HostingSpaces\eeeaim\edwardchristianchurch.org\wwwroot\Test-Kitchen\Mass_Text\text_engine.php on line 41
OOPS! Something went wrong
Just tell me how to correct line 41. or what to add elsewhere.
Please do not tell me to use phpmailer, unless you tell me exactly (in non technical terms) which lines to change and how, as it results in error 404 with no info as to what file/directory is missing.
Note: sender is constant. recipients are constant (subscriber list)
Your implementation is completely wrong. You are using the inbuilt PHP mail function which sends email using the sendmail protocol from your server mostly found in /usr/bin/sendmail for linux. If you need to send email using the SMTP protocol, please use extended libraries like PHPMailer or SwiftMailer. SMTP's are generally slow than API's but they are the most easily available option. This is the most widely used SMTP library for PHP. The link shows a demo and various options you can set with it. Good luck.
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 use jQuery validate form plugin to receive email from my form contact. My code seems to run but I don't get mail. I see the message "Form sent" in WAMP. I have configured my SMTP server and I don't have error message from WAMP.
My form : http://jsfiddle.net/Xroad/2pLS2/24/
What's wrong ?
<?php
if(isset($_POST) && isset($_POST['form_firstname']) && isset($_POST['form_name']) && isset($_POST['form_email']) && isset($_POST['form_telephone']) && isset($_POST['form_message'])) {
extract($_POST);
if(!empty($form_firstname) && !empty($form_name) && !empty($form_email) && !empty($form_telephone) && !empty($form_message)) {
$to = "XXXXXX#gmail.com"; // My real email
$subjet = "Contact from the site";
$msg = stripslashes($form_message);
$msg = "A question came \n
Firstname : $form_firstname \n
Name : $form_name \n
Email : $form_email \n
Message : $form_message";
mail($to, $subjet, $msg);
echo "Form sent";
} else {
echo "You have not filled in the field";
}
}
?>
<form id="form-general" action="php/traitement.php" method="post">
Well, in case a simple mail() function with no other code is not delivering mail to your inbox it seems like your SMTP has more to be configured.
Make mail("XXXXXX#gmail.com", "Contact from the site", "test") work first and the rest of your code is probably good to go.
UPDATE
SMTP default value is localhost. According to me you're not able to resolve the domain SMTP (the one that is currently in your php.ini)! You could check that in console with telnet SMTP 25 (you might need to enable telnet first).
Anyway make sure you have a MTA on the other side - my best guess would be you need to call your system administrator and ask him about your SMTP host and port.
Just to keep you alert - in case you need to authenticate against your MTA you'll have to find another solution, because php's mail() (according to my knowledge) can't do that. Search for SwiftMailer and PhpMailer, both coming with a lot of examples.
Update 2
Well, sendmail.exe comes with its own configuration file, where you need to enter your smtp server, port and credentials!
Update 3
sendmail.exe TLS support hasn't been updated since 2008. The windows version has its last update 3 years ago and depending on which download you've chosen you might end up with even older version. While it works in general, there have been a number of reports for problems.
Even the author sendmail.exe is recommending MSMTP as a great open source alternative, and even blat as a more powerful tool. The only drawback I see is that those have a little more options and use different configuration format, which might look like harder to be configured.
i think you have problem with your host, so we try to use gmail SMTP server to send emails.
in order to do so, i'm going to use an email library called Swift, download it free here http://swiftmailer.org/ once you downloaded it just rename the folder into swift and setup the $smtp_settings username and password (the one you use to login into your gmail account) and be sure that the required file
require_once dirname(__FILE__).'/swift/lib/swift_required.php';
is in the right path. (as it is now you can simply have the swift folder in the same root of the mail file)
<?php
function send_mail($to, $from, $subject='', $body='', $smtp=array()){
// be sure this point where the swift package is...
require_once dirname(__FILE__).'/swift/lib/swift_required.php';
$settings = (object)$smtp;
$transport = Swift_SmtpTransport::newInstance($settings->host, $settings->port, $settings->encryption)
->setUsername($settings->username)
->setPassword($settings->password);
$mailer = Swift_Mailer::newInstance($transport);
$_from = is_array($from) ? $from : array($from);
$_to = is_array($to) ? $to : array($to);
$message = Swift_Message::newInstance($subject)
->setFrom($_from)
->setTo($_to)
->setBody($body);
$result = $mailer->send($message);
return $result;
}
if(isset($_POST) && isset($_POST['form_firstname']) && isset($_POST['form_name']) && isset($_POST['form_email']) && isset($_POST['form_telephone']) && isset($_POST['form_message'])) {
extract($_POST);
if(!empty($form_firstname) && !empty($form_name) && !empty($form_email) && !empty($form_telephone) && !empty($form_message)) {
// SMTP Server Configuration
$smtp_settings = array(
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'XXXXXXXXXXX#gmail.com',
'password' => '************',
);
// Send an email to client and a copy to us...
$to = 'XXXXXXXXXXX#gmail.com'; // who receive
$from = array('XXXXXXXXXXX#gmail.com' => 'My Site Name'); // who send
$subject = 'Contact from the site';
//$message = stripslashes($form_message)."\r\n";
$message = 'A question came'."\r\n";
$message .= 'Firstname : '.$form_firstname."\r\n";
$message .= 'Name : '.$form_name."\r\n";
$message .= 'Email : '.$form_email."\r\n";
$message .= 'Message : '.$form_message."\r\n";
if(send_mail($to, $from, $subject, $message, $smtp_settings)){
echo "Email sent";
} else {
echo "Email not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
I want, that users who register on my site, have to activate their account first, before using it. The problem is, that I don't get any email to my email test account.
Before I start posting a code, could the problem be, that I'm working currently on a local machine with xampp?
Otherwise, here is the code snippet
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$insertMailVerify = $this->db->prepare("INSERT INTO mailverify (mailAddress, token, datetime) VALUES (:mailAddress, :token, :date)");
$insertMailVerify->execute(array(':mailAddress'=>$emailAddress,
':token'=>$random,
':date'=>$date));
$to = $emailAddress;
$subject = "Activating your Account";
$body = "Hi, in order to activate your account please visit http://localhost/FinalYear/activation.php?email=".$emailAddress." and fill in the verification code $random";
if(mail($to, $subject, $body))
{
echo ("<p>Message success</p>");
}
else {
echo ("<p>Message fail</p>");
}
Just in case you wonder where i take $emailAddress from: This is just a code snippet, i already let the software echo the email address, and it's correct. It even goes in the "Message success" if case, but I still can't get any email. What could be the problem?
After submit the form you can use a code or link and send it to the user email id
$message = "Your Activation Code is ".$code."";
$to=$email;
$subject="Activation Code For Talkerscode.com";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link Verify.php?id='.$db_id.'&code='.$code.'to activate your account.';
$headers = "From:".$from;
mail($to,$subject,$body,$headers);
echo "An Activation Code Is Sent To You Check You Emails";
Code from TalkersCode.com for complete tutorial visit http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php
in local host i think the best way is using phpmailer and gmail account
here the tutorial : http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
It seems your mail server SMTP is not configured correctly....
check the port and IP of SMTP server address.
Try to change you PHP.ini file like this and tell me if it works.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465 //if 465 is not working then try 25
If this is not working then there are a lot of tutorials of how to achieve what you are trying to do.
Here is one link: Send email from localhost
Had the same problem, but on linux.
1) Assuming your mail() function is working properly: the problem could be, that email is coming into spam-box (because these localhost mail systems are often marked as spambots, so email services are protecting emails from unverified host).
2) If mail() is not working, its not configured properly, if you follow some tutorial and configure it, which needs like 5 minutes, you will realise why not to use it:)
The best way for you is to use some free or paid smtp service. Very easy, quick and your emails wont be marked as spam. And install PEAR library to send email. Here is an example.
$from = 'from#domain.com';
$to = 'to#domain.com';
$subject = 'subject';
$body = 'Hello!';
$host = 'smtpserver.com';
$port = '25';
$username = 'yourlogin';
$password = 'yourpass';
$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>");
}
To your verification code: instead of telling user to write down the verification code, better generate him the link, that he clicks his account gets activated. Something like
http://localhost/FinalYear/activation.php?email=some#email.com&token=79054025255fb1a26e4bc422aef54eb4
on registration page: generate the activation token like md5($email . '_sometext');
on activation page if(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. }
please note, that it is just an example, there are 10 ways how it could be done :)
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.