I'm working on a registration page on a website where I need to implement a email verification.
I have installed PHPMailer-master on the server. The first part of the code is written directly in the registration php:
include('send_mail.php');
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> thank you for joining us! Please verify your email and get started using your account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, a verification email has ben sent to you. Please activate this to get started.";
And the code in send_mail.php:
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
$from = "*mywebsite*";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "*my webhost's mailserver*"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*username*"; // SMTP username
$mail->Password = "*password*"; // SMTP password
$mail->SetFrom($from, '*mywebsite?*');
$mail->AddReplyTo($from,'*mywebsite?*');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
I'm fairly sure the information in send_mail.php is correct. I'm using the information I found on my configure mail client page on cPanel ("Secure SSL/TLS Settings").
When I try to register, the website goes white somehow. The account gets created on phpmyadmin. No error-log in public_html.
Any ideas?
UPDATE: Sorry for late response.
I've tried using the PHPMailer Test Page. When I try SMTP, the page just loads forever. When using Sendmail, I get the error "Could not execute: /usr/sbin/sendmail -t -i". When I use the QMAIL, it says the email was sent but I never recieve it.
BUT... when I use Mail, I recieve it successfully. Is "Mail" secure enough to use? What are the differences? And how should I implement it into the php code of the register page? I can't find any specific part in the PHPMailer Test Page code for the "mail" function.
Code looks like this now:
$email = $_POST['email'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('myemail');
$mail->AddAddress($email);
$mail->SetFrom('myemail');
$mail->AddReplyTo('myemail');
$mail->Subject = 'subject';
$mail->Message = 'message comes here';
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
Error message: "Message body empty"
Related
I finally find the solution of my problem, i post new codes that work properly. I have a new issue now, let say that my email has more than one destination, when i try to put many email adresses in $to variable. For exemple $to="abc#gmail.com,pat#gmail.com"; it's not working but with only one it's worked. How can i do it with more than one address ?
$account="username";
$password="password";
$to="abc#gmail.com";
$from="efb#gmail.com";
$from_name="name";
$msg="<strong>test smtp with amazon.</strong>"; // HTML message
$subject="HTML message";
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPAuth= true;
$mail->Port = 465;
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
As u are using gmail u should less secure your app using this link after login to gmail.
link: https://www.google.com/settings/security/lesssecureapps
host should be $mail->Host = "smtp.gmail.com"; for gmail.
Do you completed validate e-mail abc#gmail.com ? First you must go to
> https://console.aws.amazon.com/ses/
Then in Email Addresses checked there is your abc#gmail.com address. If no click on Verify a New Email Address and add one. You'll get test message. After that in Verified Sender: Email row you will see next to your e-mail the green world "verified".
For adding more than one email you can do it in the following way:
$mail->AddAddress("prodip#cottonist.org");
$mail->AddAddress("srasel84#yahoo.com");
$mail->AddAddress("abid#cottonist.org");
Instead of using php's mail() function, I've been trying to set up PHPMailer with no success. I put in "echo here" for debugging purposes, and that is all it shows. I do not get any emails, or the sent or error messages. I'm stumped, and after researching it on here may switch to swift mailer. I'd really like to know what I screwed up though.
In my code, address is set to my email, and the username and password are set to a dummy account I made.
<?php
include('class.phpmailer.php');
$mail = new PHPMailer();
$address = "test#gmail.com";
$body = "test email";
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "username#gmail.com";
$mail->Password = "password";
$mail->SetFrom('name#yourdomain.com', 'Web App');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML($body);
$mail->AddAddress($address, $name);
echo "Here";
if($mail->Send()) {
echo "Message sent!";
}
else {
echo "Mailer Error: " ; $mail->ErrorInfo;
}
?>
As you are using Gmail I think you must make sure the gmail account has the insecure application auth activated
i am using PHPmailer to send emails
here is code that i have used:
$mail = new PHPMailer();
$subject = "test";
$to = "test_patel#yahoo.com"
$mail->SetFrom("PDSociety#aol.com","Punjab Dental Society");
$mail->AddReplyTo("PDSociety#aol.com", "Punjab Dental Society");
$mail->Subject = $subject;
$mail->MsgHTML($str);
$mail->AddAddress($to, "Punjab Dental Society");
if(!$mail->Send())
{
$err = "Mailer Error: " . $mail->ErrorInfo;
//echo $err;
} else {
$msg = "Message sent!";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
if i change email address from yahoo to gmail or hotmail, still email are not sent.
i checked by echoing error, but no errors.
can anyone explain what is the issue ?
After trying various ways, i found following code working with almost all email providers
$to['email'] = "recipients email address";
$to['name'] = "name";
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = 'xyz#domainname.com';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('xyz#domainname.com', 'any name');
$mail->AddAddress($to['email'],$to['name']);
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;
}
$mail->ClearAddresses();
variable values needs to be changed accordingly.
Hope these helps people having issues with PHPmailer
PHPMailer is only involved in submitting the message to your own mail server, and you're not having any problem there. After that, your mail server takes on the responsibility of sending it on, so you will find the answer in your mail server's logs.
There is no simple way to ensure messages end up in the inbox and not spam - if there was, spammers would be using it and filtering would be useless. Make sure your DNS resolves backwards and forwards, that you have valid SPF records, that you sign your messages with DKIM (especially important for Yahoo) and most importantly, that you don't send messages that your recipients think are spam.
Try this :
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->AddAddress($to['email'],$to['name']);
$mail->FromName = '';
$mail->Subject = $subject;
$mail->MsgHTML($message);
$send = true;
return $mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
$e->getMessage(); //Boring error messages from anything else!
}
It will help you if any exception error.
Have you looked on the post here: Using PHPMailer Results in many blocked emails? The asker solved the issue by changing the email subject:
Well I solved the issue; the code above was not the problem and works
great.
In my subject, I used a phrase regarding "verify your account
information" and that got it blocked on a few ISP's.
So the lesson is, your subject matters. I was looking at my php code
and my body content before I realized this.
The content of the email and its subject can make ISPs ban it. You could try taking the content of one of your received emails from your inbox and see if that goes through.
I'm really new to this field. I am working on my portfolio. I have made modal contact form (bootstrap) for "contact me". I'm not getting mails on my gmail account from php . I'm using PHP mailer and following is the code of my php with file name contact-form.php:
<?php
// check for form submission - if it doesn't exist then send back to contact form**
if (!isset($_POST['submit'])) {
$message=
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['contact-message'];
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Set up SMTP connection
$mail->SMTPAuth = true; // Connection with SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host ="smtp.gmail.com"; //Gmail SMTP address
$mail->Port = 465; // Gmail SMTP port No idea what this is suppose to be
$mail->Encoding ='7bit';
$mail->Username = "mygmailid#gmail.com"; // Gmail address
$mail->Password ="Password"; // Gmail Password
// Compose
$mail->SetFrom($_POST['email'],$_POST['name']);
$mail->AddReplyTo($_POST['email'],$_POST['name']);
$mail->Subject = "Mail from Portfolio";
$mail->MsgHTML($message);
//Send To
$mail->AddAddress("recipientmail#gmail.com", "Recipient Name");
$result = $mail->Send();
$message = $result ? 'Successfully Sent!' : 'Sending Failed';
unset($mail);
}
I have linked all my html pages with contact-form.php as:
<form action="contactform.php" class="form-horizontal" method="post" enctype="text/plain">
When I clicked on the send message button it's directing me to the contact-form.php but nothing is happening.
Please Please help me out I have been stuck in this issue since last week.
Any suggestion or help will be highly appreciable.
Thank you in advance :)
Tayyaba.
i appreciate your code you have applied so far, as it is hard to debug your code, it would be better if you test your code with static info,
i have given a working example below:
$email = new PHPMailer();
$email->From = 'amrinder.salentro#gmail.com';
$email->FromName = 'Amrinder Singh';
$email->Subject = 'Hello';
$email->Body = "How are you karan";
$email->AddAddress( 'amrinder.salentro#gmail.com' );
$email->AddAttachment( 'PATH_OF_YOUR_FILE_HERE' , 'NameOfFile.pdf' ); //optional
return $email->Send();
i hope it would help you. please go with it...
thanks... :)
First of all, I suggest you start again using a known-good example for sending through gmail, and make sure you're using latest PHPMailer. As for your script, this line means that your mail handling code will only ever be run when the form is not submitted, and since this will result in sending errors, which you are not showing, you won't see anything happen!
if (!isset($_POST['submit'])) {
This should be:
if (isset($_POST['submit'])) {
I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');