I'm using codeigniter email library to send mail. The following is my email.php library file configuration
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.mailhostbox.com';
public $smtp_user = 'myemailid';
public $smtp_pass = 'mypassword';
public $smtp_port = 25;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = '';
public $wordwrap = TRUE;
public $wrapchars = 76;
public $mailtype = 'html';
public $charset = 'iso-8859-1';
and here is my send email function in controller:
$message = '<div>test</div>';
$this->email->message($message);
$this->email->to(ADMIN_EMAIL);
$this->email->from($email, $userName); // if $email = 'myemailid', then its working
$this->email->subject('SenderID Request');
if($path != '')
$this->email->attach(USER_PROFILE_PATH.$path);
if($this->email->send()){
$result = $this->User_model->senderIDStatus($senderIDID, 3);
if($result == 1) {
$this->session->set_flashdata('Status','success');
$this->session->set_flashdata('Message','SenderID submitted for approval, we will notify you soon');
}
else {
$this->session->set_flashdata('Status','failure');
$this->session->set_flashdata('Message', 'An error occured, please try again');
}
}
else {
print_r($this->email->print_debugger());
}
I can send email if, $this->email->from() is myemailid, which is set in email library. If I tried to send email from another email id it shows an error like
The following SMTP error was encountered: 553 5.7.1 : Sender address rejected: not owned by user myemailid
The mail server is rejecting your email because you're trying to send from a username which differs from the username you're logging in as, e.g. you've set the From: to be no-reply#mydomain.com, which is obviously not the same as myemailid
Related
I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.
I have added a registration and login system to this application. I am current working on a password reset system.
I was able to do these 2 things separately:
Send a password reset email containing dummy text.
Create a valid password reset link.
I was unable however, to send the email once the reset link was inserted into the email body.
Here is he controller:
class Newpassword extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private $sender_email = "noreply#yourdomain.com";
private $sender_name = "Razvan Zamfir";
private $user_email = '';
private $subject = 'Pasword reset link';
private $reset_token = '';
private $reset_url = '';
private $reset_link = '';
private $body = '';
public function index() {
// Display form
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['tagline'] = 'Reset your password';
$data['categories'] = $this->Categories_model->get_categories();
// Form validation rules
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if(!$this->form_validation->run()) {
$this->load->view('partials/header', $data);
$this->load->view('auth/passwordreset');
$this->load->view('partials/footer');
} else {
if ($this->Usermodel->email_exists()) {
//Get user email
$this->user_email = $this->input->post('email');
//create token
$this->reset_token = md5(str_shuffle($this->user_email));
//create url
$this->reset_url = base_url('changepasword/') . $this->user_email . '/'. $this->reset_token;
//create reset link
$this->reset_link = 'password reset link';
echo $this->reset_link;die();
$this->body = "Here is your $this->reset_link. \n\nAfter clicking it you will be redirected to a page on the website where you will be able to set a new pasword.";
// Send mail and rediect
$this->sendResetMail();
} else {
$this->session->set_flashdata('email_non_existent', "The email you provided does not exist in our database");
}
redirect('newpassword');
}
}
public function sendResetMail() {
// Loading the Email library
$config['protocol'] = 'sendmail';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
if(!$this->load->is_loaded('email')){
$this->load->library('email', $config);
} else {
$this->email->initialize($config);
}
// Build the body and meta data of the email message
$this->email->from($this->sender_email, $this->sender_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if($this->email->send()){
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else{
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
}
I have inspected the link, it is valid and the value of the href attribute is as intended, but once I remove echo $this->reset_link;die() I see the attempt to send the email failing:
Where is my mistake?
The email not sending issue can be from multiple reasons:
Codeigniter 3 email library is not configured correctly
test: use built in email() in php and test if that's working
php not configured correctly to send email
server is not configured for handing over emails
DNS has external mx records and local email is not forwarded out from the server
test: use external SMTP server (free gmail is fine) and configure like this the ci3 library https://forum.codeigniter.com/thread-75525-post-371979.html#pid371979
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'your_email';
$config['smtp_pass'] = 'your_password';
$config['smtp_port'] = 465;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "\r\n";
if this is not working, check what's the cause by using
if ($this->email->send(FALSE))
{
echo $this->email->print_debugger();
}
server cannot access remote ports firewalls are not opened for this
test: disable firewall or selinux and test again
Are you running it locally? If so this might be the cause of the error. Can't send emails from local machines. If not check with your hosting company if it allows you to send mails via php. Most shared hostings disable this option and sell SMTP service to allow you to send emails
Have you tried loading a view into your email function? Then you could pass the content to that file and send a html email
This is what woked for me:
public function sendResetMail()
{
// Email settings
$config['protocol'] = 'sendmail';
$config['smtp_host'] = 'mail.mydomain.com';
$config['smtp_user'] = 'myemail#mydomain.com';
$config['smtp_pass'] = '******';
$config['smtp_port'] = 465;
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "\r\n";
if (!$this->load->is_loaded('email')) {
$this->load->library('email', $config);
} else {
$this->email->initialize($config);
}
// Build the body and meta data of the email message
$this->email->from($this->sender_email, $this->sender_name);
$this->email->to($this->user_email);
$this->email->subject($this->subject);
$this->email->message($this->body);
if ($this->email->send()) {
$this->session->set_flashdata('reset_mail_confirm', "A pasword reset link was send to the email address $this->user_email");
} else {
$this->session->set_flashdata('reset_mail_fail', "Our atempt to send a pasword reset link to $this->user_email has failed");
}
}
i made a php function to send mail using phpmailer. but, there is a problem in the function. it neither send mail nor shows error. please help me out. i'm fetching mail body and some other details from other functions and its working fine except it doesn't send mail and i think there must be some problem with the host,port,username,etc.
please help me out.
thanks
my funciton:
public static function sendEmail($data) {
$r_error = 1;
$r_message = "";
$r_data = array();
$q = "select * from config where type='email_detail'";
$r = self::DBrunQuery($q);
$row = self::DBfetchRow($r);
$detail = json_decode($row['value'], true);
include "phpmailer/PHPMailerAutoload.php";
if (!empty($data['email'])) {
foreach ($data as $var) {
$work_email = 'fahadansari12feb#gmail.com'; //$var['email_id'];
$name = 'fahad'; //$var['name'];
$subject = $var['subject'];
$body = $var['body'];
$cc = $var['cc_detail'];
$bcc = $var['bcc_detail'];
$file_upload = $var['upload_file'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = '5.9.144.226'; //$detail['host'];
$mail->Port = '2222'; //$detail['port'];
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'fahadansari12feb#gmail.com'; //$detail['username']; //sender email address
$mail->Password = 'mypassword'; //$detail['password']; // sender email password
$mail->setFrom('hr#excellencetechnologies.in', 'Excellence Technologies'); // name and email address from which email is send
$mail->addReplyTo('hr#excellencetechnologies.in', 'Excellence Technologies'); // reply email address with name
$mail->addAddress($work_email, $name); // name and address to whome mail is to send
if (sizeof($cc) > 0) {
foreach ($cc as $d) {
$mail->addCC($d[0], $d[1]);
}
}
if (sizeof($bcc) > 0) {
foreach ($bcc as $d2) {
$mail->addBCC($d2[0], $d2[1]);
}
}
$mail->Subject = $subject; // subject of email message
$mail->msgHTML($body); // main message
// $mail->AltBody = 'This is a plain-text message body';
//Attach an image file
if (sizeof($file_upload) > 0) {
foreach ($file_upload as $d3) {
$mail->addAttachment($d3);
}
}
//send the message, check for errors
if (!$mail->send()) {
$row3 = $mail->ErrorInfo;
} else {
$row3 = "Message sent";
}
}
}
if ($row3 != "Message sent") {
$r_error = 1;
$r_message = $row3;
$r_data['message'] = $r_message;
} else {
$r_error = 0;
$r_message = "Message Sent";
$r_data['message'] = $r_message;
}
$return = array();
$return['error'] = $r_error;
$return['data'] = $r_data;
return $return;
}
You've not posted enough info, but I'm going to guess your problem. You're enabling TLS, but you're connecting to an IP address rather than a host name, so your host name will never match the name on the certificate and you will get a TLS validation failure on SMTP connections.
Connect to a named host with a valid certificate, or disable certificate checks (see PHPMailer docs for how to do that), though that's not recommended.
You're also using an old version of PHPMailer, so upgrade.
I cannot send mail using CodeIgniter. It displays the following error.
fwrite(): send of 28 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.
This my settings in email.php library
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.mailhostbox.com';
public $smtp_user = 'xxx#xx.in';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 25;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = '';
public $newline = "\r\n";
I could send email till yesterday. But from today morning, it shows this error.
UPDATE :
I have changed my host to gmail , but still not working. The following are the changes I made
public $useragent = 'CodeIgniter';
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
public $protocol = 'smtp'; // mail/sendmail/smtp
public $smtp_host = 'smtp.gmail.com';
public $smtp_user = 'noreply.xxx#gmail.com';
public $smtp_pass = 'xxxxxx';
public $smtp_port = 465;
public $smtp_timeout = 5;
public $smtp_keepalive = FALSE;
public $smtp_crypto = 'ssl';
public $newline = "\r\n";
But it shows error as
Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465:465 (Failed to parse address "smtp.gmail.com:465:465")
I changed the port from 465 to 587 as follows
public $smtp_port = 587;
public $smtp_crypto = 'tls';
But I got this error
Message: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
I couldn't understand what is the real issue, because it throws errors in every possible conditions
You need to initialise the config.see here
For Example
function send_email($attributes) {
$this->load->library('email');
$this->email->set_newline("\r\n");
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'host';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'user#smtp.com';
$config['smtp_from_name'] = 'FROM NAME';
$config['smtp_pass'] = 'XXX';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from($config['smtp_user'], $config['smtp_from_name']);
$this->email->to($attributes['to']);
$this->email->cc($attributes['cc']);
$this->email->bcc($attributes['cc']);
$this->email->subject($attributes['subject']);
$this->email->message($attributes['message']);
if($this->email->send()) {
return true;
} else {
return false;
}
}
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#domain.com';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'isSMTP';
public $SMTPAuth = true;
public $Username = 'email#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'ssl';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
I am Using this Code for Email.php file and it works but it sends Mail in Attachment Not in Normal Form...
Email Attachment Showing in Email
Email Attachment Output
Here is link where i am using it for verification purposes.
http://monthlyreport.ultimatefreehost.in
At index.php I am using Like this
//send email
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom(SITEEMAIL);
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
Your functions should return the values.
Use return $this->Body = $body; at the end of body(), and similarly do it for the other functions as well.
I tried sending email using smtp gmail using phpmailer. My gmail account got suspended saying that there is an unusual activity. Here is the code I have used for sending emails. What's the correct way of sending emails using smtp gmail in phpmailer?
My question is not duplicate. I have already tried this : Send email using the GMail SMTP server from a PHP page
I'm using phpmailer, and my accounts getting suspended.
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#exmaple.org';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'username#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
Here is my php code :
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom('noreply#example.org');
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;