Qweque Brako Qweque Brako - 1 year ago 68
MySQL Question

Extracting long text from database and image url from database

I am trying to display description and image(display the image directory (example.com/upload/pic.jpg) which is in my database in an email but seems not to work although other fields are showing up in the mail. Here is my code.

$toFetchEmail = $_SESSION['storedUserName'];
$email_query = "SELECT * FROM teamcaptain WHERE userName = '$toFetchEmail'";
$email_output = mysqli_query( $connection, $email_query);
if( mysqli_num_rows( $email_output ) > 0 ) {
while( $row = mysqli_fetch_assoc( $email_output ) ) {
$to_captain = $row[ 'email' ];
$captain_fName = $row["firstName"];
$captain_lName = $row["lastName"];
$captain_email = $row["email"];
$captain_phone = $row["handy"];
$captain_userName = $row["userName"];
$captain_password = $row["Password"];
$captain_email_code = $row["email_code"];
$captain_image = $row["imagesPath"];
$captain_description = $row["description"];
$captain_quote = $row["quote"];

$from = "noreply@peacevienna.com";
$subject_captain = "Team Captain Registration";
$message_captain = "Thanks for the registration as Team Captain.\r\n";
$message_captain .= "Your User Name is $captain_userName \r\n";
$message_captain .= "Your Password is $captain_password \r\n";
$message_captain .= "Your Account would be activated very soon and you can login afterwards. \r\n";
$message_captain .= "Please login into your profile using the above credentials and complete the next steps to see the necessary documents. \r\n";
$message_captain .= "Wishing you all the very best from the Management. Thanks once again for being the Team Captain. \r\n";
$fromHeader = 'From: '. $from;
$to_coordinator = "theophilusboateng7@gmail.com";
$subject_coordinator = "New Team Captain Hurray!!!";
$message_coordinator = "Congratulations. \r\n \r\n";
$message_coordinator .= "Please find the details of the new Team Captain below: \r\n";
$message_coordinator .= "First Name: $captain_fName \r\n";
$message_coordinator .= "Last Name: $captain_lName \r\n";
$message_coordinator .= "E-Mail: $captain_email \r\n";
$message_coordinator .= "Contact Number: $captain_phone \r\n";
$message_coordinator .= "Username: $captain_userName \r\n";
$message_coordinator .= "Description: $captain_description \r\n";
$message_coordinator .= "Image: $captain_image \r\n";
$message_coordinator .= "Quote: $captain_quote \r\n";

$teamcaptain = $captain_fName . ' ' . $captain_lName;

$subject = "Activate ".$teamcaptain." Account";
$message = "Dear Ana Ruiz Arevalo, \r\n \r\n";
$message .= "Please activate account by clicking through this link : http://peacevienna.com/activate.php?username=$captain_userName&code=$captain_email_code\r\n";
$message .= "Contact me if you have any issues activating any account. Thank you \r\n";
$message .= "Yours Sincerely, \r\n";
$message .= "Peace Museum Vienna \r\n";

$from = 'From: '. $from;

mail( $to_captain, $subject_captain, $message_captain, $fromHeader );
mail($to_coordinator, $subject_coordinator, $message_coordinator, $fromHeader);

// remove all session variables

// destroy the session
mysqli_close( $connection );

Answer Source

As I wrote in comments, the image requires a full URL for it and not your local machine's path, plus an img src is required and to send as HTML.


<img src="http://www.example.com/image.jpg">

Then to send as HTML, visit the PHP.net website:

It contains examples, but I will post one here and pulled from the manual on the mail() function:

// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
  <title>Birthday Reminders for August</title>
  <p>Here are the birthdays upcoming in August!</p>

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

You can basically add the extra http://www.example.com/ to your existing variable for the image. Don't forget the trailing slash at the end.


$captain_image = "http://www.example.com/". $row["imagesPath"];

and use the $captain_image variable for the img src code in your mailout.


<img src="$captain_image"> in double quotes.

However, and for your code, it would read as, and escaping the double quotes for $captain_image inside the variable declaration with \. (This is important in order to have proper HTML markup and not cause a parse error at the same time):

$message_coordinator .= "Image: <img src=\"$captain_image\"><br>";

Use <br> for line breaks when sending as HTML.

  • Sidenote: Variables do not get parsed in single quotes, so <img src='$captain_image'> will fail and will only show the variable name rather than the image.

Consider using PHPMailer, it will make your life a lot easier.

Or Swiftmailer:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download