Accept email address from user and send email to that address - php

public function send_mail() {
$this->email->from('your#email.com', 'My Name');
$this->email->to('to#email.com');
$this->email->cc('cc#email.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
i'm trying out the codeigniter email func but i cant seem to receive the test mail, but it saids that it has successfully send the mail but there's none,
new to ci so i'm confused about this problem, $this->load->library('email'); is also loaded,
anyhelp thanks!
EDIT
ok guys thanks for the answer i have added the ff code and it now works,
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'your email',
'smtp_pass' => 'your password'
);
Now my question is how will I edit the codes above to accept the "to" email from the user?
okay i did the "to" part already, and its like this
$this->email->to($point->email);
need help again with the "message" part, i need to put data from the user in the "message" part, i tried what i did in the "to" part but it only accepts one item, i need multiple items, thanks

In the section where you're calling the function send_mail(), pass the email address and other data as well, as arguments to the function; something like this:
$to = $point->email;
$msg = $point->message; // the message part
$phone = $point->phone; // phone number
$address = $point->address; // postal address
$this->send_email($to, $msg, $phone, $address);
And then slightly modify the send_mail() function as:
// $to_address will contain the value stored in $to, i.e. "abc#def.com" in this case
public function send_mail($to_address, $body_message, $phone_no, $addr) {
// can make use of extra data like phone number, address in here
$this->email->from('your#email.com', 'My Name');
$this->email->to($to_address);
$this->email->message($body_message);
.
.
.
}

It works fine in my linux server without any $config.
Please read this to set another protocol

Related

Codeigniter 4 Email sending with HTML as message

can you help me figure out a way to use the email class of CI4 to send an email with an HTML as the message?
in Codeigniter 3 it is simple and goes like this:
$email->message($this->load->view('html_page_to_send_from_view_folder',$data,true));
but in Codeigniter 4 I tried doing this:
$email->setMessage(echo view('html_page_to_send_from_view_folder',$data,true));
It gives out an error:
syntax error, unexpected echo(T_ECHO), expecting ')'
I also tried putting the view in a variable like so:
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
but it just throws the same error, I was searching all over the internet and the documentation but couldn't find a way to do this. Help please.
I tried this but got no error, and also got no email :'(
$echo_page = view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
According to this, if you want to use some template as your message body, you should do something like this:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 4 documentation states:
setMessage($body)
Parameters: $body (string) – E-mail message body
Returns: CodeIgniter\Email\Email instance (method chaining)
Return type: CodeIgniter\Email\Email
Sets the e-mail message body:
$email->setMessage('This is my message');
Okay I got it now I made it work by adding this code $email->setNewLine("\r\n"); at the end just after the setMessage:
$email->setMessage($my_message);
$email->setNewLine("\r\n");
and also I set the SMTP port 587 instead of 465:
$config['SMTPPort']= 587;
ALSO, for the setMessage, I did it like this:
$my_message = view('html_page_to_send_from_view_folder',["id" => $data['id']]);
$email->setMessage($my_message);
really weird man....
A. Firstly,
Instead of:❌
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
Use this:✅
$echo_page = view('html_page_to_send_from_view_folder',$data);
Notice the luck of an echo statement and not passing true as the third argument of the view(...) hepler function.
B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html. This can be achieved by using the setMailType() method on the Email instance. I.e:
$email->setMailType('html');
Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method. I.e:
public function sendMail(): bool
{
$email = \Config\Services::email();
$email->initialize([
'SMTPHost' => 'smtp.mailtrap.io',
'SMTPPort' => 2525,
'SMTPUser' => '479d7c109ae335',
'SMTPPass' => '0u6f9d18ef3256',
'SMTPCrypto' => 'tls',
'protocol' => 'smtp',
'mailType' => 'html',
'mailPath' => '/usr/sbin/sendmail',
'SMTPAuth' => true,
'fromEmail' => 'from#example.com',
'fromName' => 'DEMO Company Name',
'subject' => 'First Email Test',
]);
$email->setTo('to#example.com');
$email->setMessage(view('blog_view'));
$response = $email->send();
$response ? log_message("error", "Email has been sent") : log_message("error", $email->printDebugger());
return $response;
}

How do i send email through Codeigniter on some event?

I am using Codeigniter. In a User management Module i want to send an email to user if status is activated. My Email code is working but there is problem with my condition check whether his status is changed or not. I mean i want to send email only if his status is changed to Active.
Below Is My Controller Code:
if($_POST['status'] == 'active')
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'Myhost',
'smtp_port' => 25,
'smtp_user' => 'user',
'smtp_pass' => 'pass',
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('test#test.com', 'Rajan');
$this->email->to($_POST['email']);
$this->email->subject('Your Account Has Been SuccessFully Activated.');
$this->email->message('Hi, We have created your Account. Please Login ');
$this->email->send();
if ($this->email->send())
{
echo"Success";
}
else
{
echo '<p class="error_msg">That Email And Password Combination Does Not Exist !</p>';
}
}
When i edit a user and change his status it gets saved in database but the email is not fired. Please help me to solve this bug.
Please try with this if($_POST['status'] == 'Active') since your status post value is "Active" (according to our discussion). The double equal sign (==) is case sensitive when used to compare strings in PHP.

My Mails Is Always Going To Spam Folder Even Delivering Very Late In Codeigniter Php

I'm Trying To Send Mails From My Web Server Using SMTP PROTOCOL in codeigniter frame work.my mails was always delivering late and going to spam.i had follow many past conservations,still i didn't solve this problem.help me folks to solve this problem.here is my code:
$config = Array('protocol' => 'smtp',
'smtp_host' => 'http://smtp.tfas.net/',
'smtp_port' => 465,
'smtp_user' => 'info#tfas.net',
'smtp_pass' => 'xxxxxxx',
'mailtype' => 'html',
'mailpath' => '/usr/sbin/sendmail',
'charset' => 'utf-8'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
if (file_exists($attachment_path))
{
$this->email->attach("$attachment_path");
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
else
{
$this->email->from('info#tfas.net','TFAS MEMBER PORTAL');
$this->email->reply_to('info#tfas.net','Help Center');
$this->email->message($message);
$this->email->to($to);
$this->email->subject('Greetings From TFAS Member Portal');
}
if($this->email->send())
{
return true;
}
else
{
return false;
}
// Even I had gone through this link https://github.com/ivantcholakov/codeigniter-phpmailer which is a tutorial to send mails with phpmailer library in codeigniter but same problem araises..
I don't think this is an issue with codeigniter's mailer. Usually, emails are marked as spam by email service providers based on the content or an IP filter. In your case it's possible that your content appear spam-like due to not having relevant text and images. Google and most providers has a list of spam words that they look out for when evaluating the spam-ness of your emails. Check this out as it may be help you to find a useful lead: http://blog.hubspot.com/blog/tabid/6307/bid/30684/The-Ultimate-List-of-Email-SPAM-Trigger-Words.aspx

Codeigniter: unable to send email

I have a "Contact Us" page, where if users submit the form. Then email should go to the specified email.
After submiting the form i am getting the Success message ,but i am not seeing any email in my inbox/spam.
I am testing on my live server.
Pelase help me to solve my problem.
My code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class contact extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
$this->load->library('email');
$this->load->model('shopmodel');
$this->load->model('contactusmodel');
$this->load->library('form_validation');
}
function index()
{
$this->form_validation->set_error_delimiters(' <li class="errorlist">', '</li>')->set_rules('fullname', 'Name','trim|required|min_length[5]|max_length[50]|xss_clean');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('countryname', 'Country','trim|required|min_length[2]|max_length[50]|xss_clean');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('contactdetails', 'Contact Details','trim|required|min_length[40]|max_length[2000]|xss_clean');
$data=$this->contactusmodel->contactusmodel();
$data["query"] = $this->shopmodel->getshopdetailsById(199);//taking data as shop id 199
if($this->form_validation->run() === FALSE)
{
$data['ffullname']['value'] = $this->input->post('fullname');
$data['fcountryname']['value'] =$this->input->post('email');
$data['femail']['value'] = $this->input->post('countryname');
$data['fcontactdetails']['value'] =$this->input->post('contactdetails');
$this->load->view('contact/contact',$data);
}
else if ($this->form_validation->run() === TRUE)
{
$name=$this->input->post('fullname');
$sendersemail=$this->input->post('email');
$fromcountry=$this->input->post('countryname');
$message=$this->input->post('contactdetails');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'ashutosh10g#gmail.com',
'smtp_pass' => 'xxxxxxxx',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$email_body ="<div>hello world</div>";
$this->email->from('ashutosh10g#gmail.com', 'ddd');
$list = array('ashutosh10g#gmail.com');
$this->email->to($list);
$this->email->subject('Testing Email');
$this->email->message($email_body);
$this->email->send();
echo $this->email->print_debugger();
}
else{
$this->load->view('contact/contact',$data);
}
}
}
?>
What output i am getting is:
[code]
Your message has been successfully sent using the following protocol: mail
From: "ddd"
Return-Path:
Reply-To: "ashutosh10g#gmail.com"
X-Sender: ashutosh10g#gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <513e1456185d4#gmail.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_513e1456185e3"
=?utf-8?Q?Testing_Email?=
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_513e1456185e3
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
hello world
--B_ALT_513e1456185e3
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>hello world</div>
--B_ALT_513e1456185e3--
Maybe you are experimenting a relaying error somtimes the Google Server don't accept relay mail form one MTA to another inbox. Try to test to establish an SMTP dialog to your server an try to send the mail. Here is a simple SMTP dialog example:
http://www.soi.wide.ad.jp/class/20000009/slides/11/6.html
Try to send a mail if the server sends a relay error you should try with another SMTP server. If not maybe is a missconfiguration of your php.
But in my experience gmail always deny the relaying mail for spam and security reasons if the MTA is not a trusted agent.
Another reason may be that you don't have correctly configure DNS Servers and your MTA cannot find the MX Records. Example y your MTA is sendmail it notifies php tha the mail was send succesfully but if you look into sendmail logs you can find that the host is unreachable.

Zend_Mail bug emailing with $mail->setReplyTo

Why does setReplyTo($reply_to_mail) send email to $reply_to_mail? Shouldn't it just add email adress to reply-to field in the email message?
Currenyly if sending mail from website form and filling reply-to field, message sends to reply-to email and to our admin email.
Why does it duplicates email? Should send only to our admin email.
class Helper_Mail extends Zend_Controller_Action_Helper_Abstract
{
public function direct($email,$from,$message,$title,$replyto='')
{
$this->sendmail($email,$from,$message,$title,$replyto);
}
private function sendMail($email,$from,$message,$title,$replyto)
{
/* Configuring SMTP settings */
$config = array(
'auth' => 'login',
'ssl' => 'tls',
'username' => 'adminmail#gmail.com',
'password' => 'password',
'port' => 587);
$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
Zend_Mail::setDefaultTransport($smtpHost);
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($message);
$mail->setFrom('adminmail#gmail.com', $from);
$mail->addTo($email);
$mail->setSubject($title);
if(!empty($replyto))
{
$mail->setReplyTo($replyto);
}
try
{
$mail->send();
}
catch(Zend_Mail_Exception $e)
{
echo $e->getMessage();
}
}
}
You can use Zend_Mail::setReplyTo() if you are using a version of Zend > 1.8
If not (<= 1.8) you should use Zend_Mail::addHeader('Reply-To', 'replymail#example.com')
It was a bug, fixed in new versions. ;)

Categories