How can I set the default setFrom field with title in Zend_Mail
If I send mail like this
public function sendActivationEmail($id) {
$mail = new Zend_Mail();
$mail ->addTo('emailId1#gmail.com',"Recepient Name")
->setFrom("emailId1#gmail.com","Site Name")
->setSubject("My Subject")
->setBodyText("Some body msg")
->setBodyHtml("Some body msg")
->send();
}
I get an email in nice format with From Field in mail as "Site Name"
But If I send mail without setFrom, I still get the mail, but the title is "emailId1#gmail.com". I dont want to use SetFrom() just to get "Site From". Is there a way I can set it so that "Site Name" gets picked up by default ?
I have followed this tutorial for setting up my email : http://www.zendcasts.com/introduction-to-zend_mail/2010/02/
Thank you,
If you want to set a default email address for the from attribute, you can use setDefaultFrom() static method like this:
For example, in your boostrap:
protected function _initFromMail(){
// You can get the mail from an init file
Zend_Mail::setDefaultFrom('emailId1#gmail.com', 'Site Name');
}
In your function, the from adress mail is automatically fiiled.
public function sendActivationEmail($id) {
$mail = new Zend_Mail();
$mail ->addTo('emailId1#gmail.com',"Recepient Name")
->setSubject("My Subject")
->setBodyText("Some body msg")
->setBodyHtml("Some body msg")
->send();
}
I would probably just create a subclass of Zend_Mail and use that. Something like the following:
class My_Mail extends Zend_Mail
{
public function __construct($charset = null)
{
parent::__construct($charset);
setFrom('emailId1#gmail.com, 'Site Name');
}
}
Then usage would be:
$mail = new My_Mail();
$mail ->addTo('emailId1#gmail.com','Recipient Name')
->setSubject('My Subject')
->setBodyText('Some body msg')
->setBodyHtml('Some body msg')
->send();
The usual requirements about how to autoload classes in the My_ pseudo-namespace would apply. For example, you could add:
autoloaderNamespaces[] = My_
to your application/config/application.ini
Related
I just tried this example:
// for use PHP Mailer without composer :
// ex. Create a folder in root/PHPMAILER
// Put on this folder this 3 files find in "src"
// folder of the distribution :
// PHPMailer.php , SMTP.php , Exception.php
// include PHP Mailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include dirname(__DIR__) .'/PHPMAILER/PHPMailer.php';
include dirname(__DIR__) .'/PHPMAILER/SMTP.php';
include dirname(__DIR__) .'/PHPMAILER/Exception.php';
// i made a function
/*
*
* Function send_mail_by_PHPMailer($to, $from, $subject, $message);
* send a mail by PHPMailer method
* #Param $to -> mail to send
* #Param $from -> sender of mail
* #Param $subject -> suject of mail
* #Param $message -> html content with datas
* #Return true if success / Json encoded error message if error
* !! need -> classes/Exception.php - classes/PHPMailer.php - classes/SMTP.php
*
*/
function send_mail_by_PHPMailer($to, $from, $subject, $message){
// SEND MAIL by PHP MAILER
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Use SMTP protocol
$mail->Host = 'your_host.com'; // Specify SMTP server
$mail->SMTPAuth = true; // Auth. SMTP
$mail->Username = 'my_mail#your_host.com'; // Mail who send by PHPMailer
$mail->Password = 'your_passord_of_your_box'; // your pass mail box
$mail->SMTPSecure = 'ssl'; // Accept SSL
$mail->Port = 465; // port of your out server
$mail->setFrom($from); // Mail to send at
$mail->addAddress($to); // Add sender
$mail->addReplyTo($from); // Adress to reply
$mail->isHTML(true); // use HTML message
$mail->Subject = $subject;
$mail->Body = $message;
// SEND
if( !$mail->send() ){
// render error if it is
$tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
echo json_encode($tab);
exit;
}
else{
// return true if message is send
return true;
}
}
/*
*
* END send_mail_by_PHPMailer($to, $from, $subject, $message)
* send a mail by PHPMailer method
*
*/
// use function :
send_mail_by_PHPMailer($to, $from, $subject, $message);
When calling new PHPMailer() the system throws a error:
Can't find class PHPMailer.
The include of the files works fine. What is wrong?
You could try reading the readme (which should be the first place you look anyway), which tells you the right way to load PHPMailer without composer.
I expect the problem is that you are using include instead of require, and the classes are failing to load, so that when you try to make an instance, it's not defined, giving you the error you're seeing. Your script will not work without those files, so you want it to fail if they can't be loaded.
There's a lesson though - learn to use composer and understand how it works. As a PHP novice, it's the single best thing you can do.
I am sending emails from my laravel website using SMTP. When i send email to the user then i want to copy that mail to IMAP sent folder too.
Here is my code when i am sending mail to user:
$mail = Mail::to($this->receiver)
->send(new ComplaintMail($this->sender->user_email,$this->subject,$this->complaint,$this->answers,$this->sender));
$path = "{mypath.com:993/imap/ssl}Sent";
$imapStream = imap_open($path,$this->sender->user_email,$this->sender->email_password);
$result = imap_append($imapStream,$path,$mail->getSentMIMEMessage());
imap_close($imapStream);
Also I tried using imap_mail_move() method like so:
$mail = Mail::to($this->receiver)
->send(new ComplaintMail($this->sender->user_email,$this->subject,$this->complaint,$this->answers,$this->sender));
$path = "{mypath.com:993/imap/ssl}Sent";
$imapStream = imap_open($path,$this->sender->user_email,$this->sender->email_password);
imap_mail_move($imapStream,$mail,$path);
imap_close($imapStream);
Both ways it didn't worked out
In ComplaintMail class, build function looks like :
public function build()
{
return $this->from($this->sender)
->subject($this->subject)
->markdown('emails.complaint');
}
i am trying to understand why my subject method in index.php triggers an error of not being defined.i am using phpmailer 5.2.7 with php 7.2 and wampserver 3.1.7
//here is my extended class from phpmailer//
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'xxxxxx#gmail.com';
public $FromName = MM;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'xxxxxxx#gmail.com';
public $Password = 'xxxxxx';
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();
}
}
and here is part of my index page where i have defined my variables
$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 PHPMailer(true);
$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;
Firstly, why are you using a version of PHPMailer that's literally years out of date? Get the latest, which has new features, fixed bugs, and fewer security holes. While you're upgrading, consider switching to using composer to manage your dependencies.
The problem you're having is quite straightforward: you have created a subclass that adds the subject() method, but the instance you've created in your script is of the original PHPMailer class, not your subclass. Do this instead:
$mail = new Mail(true);
Naming your class with a very generic "Mail" name is very likely to bring you an unexpected lesson on why namespacing is a good idea, so I'd recommend adding a namespace for your app to avoid name clashes.
While it's a good idea to subclass like this to set default values easily, it's also inviting you to check in credentials to your source repo, which is usually a bad idea. Better to use your child class to read those values from an environment file ("dot env") using a package like this.
I use swift mailer for send email, and after send email in box I have mail without html, like in screen
but in another mail services everything fine
this my code
public function createMessage($subject, $receivers, $template, $context)
{
$message = \Swift_Message::newInstance($subject);
$message->setFrom($this->from_address);
$message->setTo($receivers);
$body = $this->twig->render($template, $context);
$plaintext = strip_tags($body);
$message->setBody($body, "text/html");
$message->addPart($plaintext, "text/plain");
$this->mailer->send($message);
}
What problem in this code? I set body test/html. What problem not understand
Try with this:
public function __construct(Container $oContainer) {
$this->oContainer = $oContainer;
}
$this->oContainer->get('templating')->render()
And set the content yourself:
$message->setContentType('text/html');
I want to send a html page as message when sending email in codeigniter..
$this->email->set_mailtype("html");
$this->email->from('xyz#gmail.com', 'ABC');
$this->email->to($this->input->post('emailid'));
$this->email->subject('New Subject');
$message = //HERE I WANT TO INCLUDE A FILE TO END AS MESSAGE
$this->email->message($message);
$this->email->send();
Could it be possible ??
Please help...
You can simply use the method file_get_contents()
$message = file_get_contents("/path/to/htmlfile");
Also you can use the codeigniter way
$template = $this->load->view(APPATH.'email/file', $your_data, true);
What I have been using in my project, I created a function in a common_helper named as sendMail
function sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName)
{
$CI =& get_instance();
$CI->load->library('email');
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$CI->email->clear(TRUE);
$CI->email->initialize($config);
$CI->email->from($mailFromId, $mailFromName);
$CI->email->to($mailTo);
$CI->email->subject($subject);
$CI->email->message($mailContent);
$CI->email->send();
}
You can call this function anywhere from your controller to send mail. Regarding HTML content of email should be loaded as Agam Banga mentioned above:
$mailContent = $this->load->view('email/template', $data, true);
This varibale can be passed simply calling like
sendMail($subject, $mailContent, $mailTo, $mailFromId, $mailFromName);
In your controller.
Let me know if you face any issue.
Here the code of Model and sending mail Using Email Library. And auto load library use it on your config folder -> autoload file
$autoload['libraries'] = array('email');
public function mail_send($mdata){
$this->load->library('email');
$this->email->set_mailtype('html');
//$this->email->set_newline("\r\n");
$this->email->from($mdata['email']); // change it to yours
$this->email->to('xyz#gmail.com');// change it to yours
$this->email->cc($mdata['email']);
$this->email->subject('Hello');
//$this->email->message($mdata['address']);
$body= $this->load->view('pages/mail', $mdata, true);
$this->email->message($body);
// echo '<pre>';
// print_r($mdata);
// exit();
$this->email->send();
$this->email->clear();
}