PHPMailer send more values - php

I am profun in PHP, so i need help with this one:
I added new variables to PHPmailer form, however How can i manage to send them to email?
this is the code I think I must modify:
$mail = new PHPMailer();
$mail->From = $conf['email_sent_from'];
$mail->FromName = $conf['email_sent_from_name'];
$mail->Subject = $conf['email_subject'];
$mail->WordWrap = 50; // some nice default value
$mail->Body = $values['message']; <-- this code
$mail->AddReplyTo( $values['email'] );
$mail->AddAddress( $conf['email_to'] );
Those are all values:
$values = array(
'webname' => $_POST['webname'],
'business' => $_POST['business'],
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'message' => $_POST['message'],
);
I want webname, business, name, email, phone, & message to be in the body. How can i do this?
AND is there a way of HTML customization? like adding < br >

cheap and dirty with minimum formatting
$mail->Body = implode('<br>',$values);
a little more formatting, make sure html email is used
$body="<table>";
foreach ($values as $k=>$v){
$body.='<tr><td>'.$k.'</td><td>'.$v.'</td></tr>';
}
$body.="</table>";
$mail->IsHTML(true); // its html mail baby
$mail->Body =$body;

Related

Problem with sending all emails to the same person when sending email with PHPMailer

I am trying to send mail to multiple emails with PHPMailer. First, I listed people's names from the table. Then I used the loop to send emails to those people. but the problem is that everyone's information goes to all emails.
PHPMailer
foreach ($id as $mailId) {
$connect->connect('account where id=:id', array('id' => $mailId), '', 0);
$users = $connect->connect->fetch(PDO::FETCH_ASSOC);
$name = $users['name'];
$mailAdres = $users['mail'];
// ob_start();
// include("PHPMailer/template.php");
// $mail_template = ob_get_clean();
$mail_template = $name;
$mail->addAddress($mailAdres, $name);
// $mail->addBCC($mailAdres, $name);
//Content
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $odemeType;
$mail->Body = $mail_template;
$mail->AltBody = '';
$mail->send();
}
You need to clear the current address once the mail is sent otherwise you are adding a new email to the existing send list each time round the loop.
foreach ($id as $mailId) {
$connect->connect('account where id=:id', array('id' => $mailId), '', 0);
$users = $connect->connect->fetch(PDO::FETCH_ASSOC);
$name = $users['name'];
$mailAdres = $users['mail'];
$mail_template = $name;
$mail->addAddress($mailAdres, $name);
// $mail->addBCC($mailAdres, $name);
//Content
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $odemeType;
$mail->Body = $mail_template;
$mail->AltBody = '';
$mail->send();
// clear this email address before continuing the loop
$mail->clearAddresses();
}
Note that will clear only the to address, if you also use cc and bcc you may need to do
//Clear all recipients (to/cc/bcc) for the next iteration
$mail->clearAllRecipients();

Yii send email to multiple recipients

hello im using yiimail here. i want to sent an email to multiple recipients
here is my code
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
// $mail->setTo(array($emailReceiver));
$mail->setTo($model->receiver1);
$mail->setCC($mailCC);
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
$mailCC get input value from user and $model->receiver1 from database. if user input 2 other user for $mailCC, this only send to the first email, not both.
ex:
$model->receiver1=email1#mail.com
$mailCC = array("email2#mail.com", "email3#mail.com") //this is from user input
the email will only send to email1#mail.com & email2#mail.com
i've tried
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
// $mail->setTo(array($emailReceiver));
$mail->setTo($model->receiver1);
$mail->setCC(array($mailCC)); //this one with array
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
but it return this error
preg_match() expects parameter 2 to be string, array given
where did i do wrong?
You can try this
$mail->addCC($mailCC[0]);
$mail->addCC($mailCC[1]);
Hopefully it will work. Here is my complete running code
$message = new YiiMailMessage;
$message->view = 'registrationFollowup';
//userModel is passed to the view
$message->setBody(['userModel' => "test"], 'text/html');
$message->setTo("xxxx#gmail.com");
$message->addCC('yyyy#gmai.com');
$message->addCC('zzz#gmai.com');
$message->from = "pqr#gmail.com";
$status = Yii::app()->mail->send($message);
print_r($status); //print the number of recipient, which 3
i got this problem done
here's my code
$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
foreach($mailcc as $to){
$mail->addTo(trim($to));
}
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);
we should explode separator, loop recipients and change to addTo not setTo
thanks to u guys that have give some solution

PHPMailer works where CakeEmail doesn't with the same configuration

I have the following problem, as you may have imagined by the title I have a CakePHP v2.5.6 application with a contact form, and it's giving me an authentication error every time I submit it, how was my surprise, after trying a simple test using PHPMailer it works perfectly with apparently the same configuration.
CakePHP configuration (app/Config/email.php)
<?php
class EmailConfig {
public $info = array(
'transport' => 'Smtp',
'host' => 'smtp.foo.com',
'port' => 25,
'username' => 'username',
'password' => 'password'
);
}
CakePHP sender code
CakeEmail::deliver('foo#foo.es', 'Subject', 'Test', 'info');
CakePHP error report
PHPMailer test script
<?php
require './PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.foo.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->Port = 25;
$mail->setFrom('info#example.es', 'Mailer');
$mail->addAddress('foo#foo.es', 'Mr. foo');
$mail->addReplyTo('info#example.es', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
So my question is, is there actually any differences between configurations, or I'm missing something? Why is PHPMailer working and CakeEmail isn't?
Thank you in advance :)
I found the solution which seems pretty dummy but the debug information was not completly accurate about the error cause.
The thing is missing and where PHPMailer and CakeEmail differ is in the from config field.
PHPMailer from configuration
$mail->setFrom('info#example.es', 'Mailer');
As you may notice the CakeEmail config file lacks of that field, so just adding it the error disappears.
The final CakeEmail config file should look like:
<?php
class EmailConfig {
public $info = array(
'transport' => 'Smtp',
'host' => 'smtp.foo.com',
'port' => 25,
'username' => 'username',
'password' => 'password',
'from' => 'info#example.es'
);
}
I hope this helps someone :)

Using SMTP PHPMailer for 'Domain' email address, email sent but not recieved

I am using PHPMailer to send email. It works fine when I use gmail smtp but when I try using my domain smtp what I see on screen is 'message sent!' but I don't receive any email at all. I have tried using debugger, it says
We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
and here is my code
<?php
date_default_timezone_set('Etc/UTC');
//Load PHPMailer dependencies
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
/* CONFIGURATION */
$crendentials = array(
'email' => 'xxxxx#example.com',
'password' => 'xxxxxx'
);
$smtp = array(
'host' => 'secure.ehostpk.com',
'port' => 465,
'username' => $crendentials['email'],
'password' => $crendentials['password'],
'secure' => 'ssl' //SSL or TLS
);
/* TO, SUBJECT, CONTENT */
$to = 'xxxxxx#example.com'; //The 'To' field
$subject = 'This is a test email sent with PHPMailer';
$content = 'This is the HTML message body <b>in bold!</b>';
$mailer = new PHPMailer();
//SMTP Configuration
$mailer->isSMTP();
$mailer->SMTPDebug = 2;
$mailer->SMTPAuth = true; //We need to authenticate
$mailer->Host = $smtp['host'];
$mailer->Port = $smtp['port'];
$mailer->Username = $smtp['username'];
$mailer->Password = $smtp['password'];
$mailer->SMTPSecure = $smtp['secure'];
//Now, send mail :
//From - To :
$mailer->From = $crendentials['email'];
$mailer->FromName = 'Team'; //Optional
$mailer->addAddress($to); // Add a recipient
//Subject - Body :
$mailer->Subject = $subject;
$mailer->Body = $content;
$mailer->isHTML(true); //Mail body contains HTML tags
//Check if mail is sent :
if(!$mailer->send()) {
echo 'Error sending mail : ' . $mailer->ErrorInfo;
} else {
echo 'Message sent !';
}
I have searched a lot and I don't understand what should I do. Any help would be greatly appreciated.
One thing that I have found is, that on most servers these days (especially cpanel servers) the email address you are attempting to send with actually has to be created within cpanel itself in order for it to work properly.
From my understanding, the script only routes it to the server and the email account, and then it's routed further from there and finally sent out from your email account on the server. So a solution may be to actually setup the email address in your server.

Cakephp SMTP emails syntax error

I've got a problem with email sending in cake. My method looks like this:
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'auth' => true,
'host' => 'ssl://smtp.gmail.com',
'username'=>'mymail#gmail.com',
'password'=>'mypass',
);
$this->Email->from = "admin#localhost";
$this->Email->to = "my_test_mail#centrum.cz";
$this->Email->subject = "Test";
$this->Email->sendAs = "text";
$this->Email->delivery = 'smtp';
$this->Email->send('Hello message body!');
But when I try to send the email I get:
555 5.5.2 Syntax error. l3sm512374fan.0
What do I need to chnage in order for this to work?
Thanks
Per RFC2821, to which Google's SMTP servers seem to be a stickler on, the format of the email addresses should be in the following way:
Recipient Name <myname#example.com>
-or-
<myname#example.com>
Do this for both the from and to address, and you should be good to go. If you don't have the name of the user, then you can just repeat the email:
$this->Email->to = "my_test_mail#centrum.cz <my_test_mail#centrum.cz>";
-or-
$this->Email->to = "<my_test_mail#centrum.cz>";

Categories